Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Collectable coins doesnt collect besides only one?

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?

2 comments

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?

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".

1 answer

✓ Accepted answer

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!

3 comments

0

It may require a debounce but I know it's not your fault.

0

Thanks a lot!

0

Glad to help.

This discussion is preserved as it was. New answers, comments, and votes are closed.