Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Collectable coins doesnt collect besides only one?

Asked by 5 years ago
Edited 5 years ago

Here is the script for a coin collecting

local coin = workspace.Coin

coin.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid")then

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

player.Leaderstats["Coins"].Value = player.Leaderstats["Coins"].Value + 1

coin:Destroy()

end

end)

However there is no other collectable coin besides the only one. They all have the same name. Anyone has a idea how can i fix this or improve my script?

0
Could you try changing the names of the coins, and could you tell me, are you running this in one script, or in all of the coins? KDarren12 705 — 5y
0
Line 1 finds only the first instance by the name "Coin". You would have to use GetChildren() to retrieve every child of workspace and check for every child by the name "Coin". DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Assuming you have one script controlling all of the coins, you are only defining one coin, in which case all the others are left alone. To fix this, you can make a folder in your workspace named something like "CoinStorage" and throw all your coins in there, and reference them by using the function :GetChildren() Here's an example of how this could be done:

local coins = workspace.CoinStorage:GetChildren() -- "CoinStorage" is a folder in the workspace containing all of the coins
for i = 1, #coins do
    local coin = coins[i]
    coin.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
            coin:Destroy()
        end
    end)
end

Hope this helps!

0
It may require a debounce but I know it's not your fault. Maginatoc -5 — 5y
0
Thanks a lot! SrOgurman 2 — 5y
0
Glad to help. MegaManSam1 207 — 5y
Ad

Answer this question