Search code examples
luaroblox

lua - my code displays no errors but doesn't work


game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    local CoinsCollected = Instance.new("IntValue", leaderstats)
    CoinsCollected.Name = "Coins"
end)

local AppleCoinRedClone = game.Workspace.AppleCoinRedClone
local Debounce = false
local Players = game:GetService("Players")

wait(1)

AppleCoinRedClone.Touched:Connect(function(PartB)
    local player = Players:GetPlayerFromCharacter(PartB.Parent)
    if not player then return end
    local leaderstats = player.leaderstats
    local CoinsCollected = leaderstats.Coins
    if PartB.Parent:FindFirstChild("Humanoid") then
        if not Debounce then
            Debounce = true
            CoinsCollected.Value = CoinsCollected.Value + 30
            wait(3)
            Debounce = false
        end
    end
end)

It works on the first coin that is inside the game by default but not on the coins after. There is only 1 coin in workspace at once.

i'm trying to, in the bottom half of the code to make it so that when the coin that spawns in 10 seconds after the previous has been collected, to add to the "coins" score by +30. I have tried re - ordering the code but it only gives me errors. the code only works on the first coin that is already in the workspace by default. Please, does anyone know the problem and can help me out?


Solution

  • Your code doesn't work because of the end) block at line six. It blocks the other lines of code, as it only should be used at the end.

    Source: https://www.lua.org/pil/4.4.html