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

Why won't coin collect on player touch?

I have a many coins in workspace, when the player touches that coin, it gives them +1 coin in their leader board.

This is the script for that (I am using remote events):

local db = true
local Coin = game.Workspace.Coin -- I have so many Coins in workspace all named "Coin"
Coin.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if game.Players:FindFirstChild(hit.Parent.Name) and game.Players[hit.Parent.Name] == game.Players.LocalPlayer then -- Checks if there's a player that touched it and if that player is the LocalPlayer
			if db == true then
				db = false
				Coin.Transparency = 1
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				game.ReplicatedStorage.GiveCoins:FireServer(player)
				-- player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
				script.Sound:Play()
				wait(1)
				Coin:Destroy()
				db = true
			end
		end
	end	
end)

But this only works if there is 1 coin in the workspace. I have many coins. But the above script only works for 1 coin.

How can I make this script work for all the coins in workspace?

Because right now. I have many coins, each one named "Coin". So shouldn't this script add a coin to the leader board whenever the player touches any of the coins?

This only works for 1 coin and not for the rest

Probably because I should only add 1 coin in the workspace because I have named inside the script "Coin". And there can only be 1 coin?

But I don't want to remove all the others coins and name them separately. It would take ages.

Thanks for any help

2 comments

0

From what you said you are using a remote event which means you are using a local script. Why don't you just use a server script instead and place it in the each coin? Or you can use a for loop https://developer.roblox.com/en-us/articles/Loops

0

I actually wanted to remove the coin from the local players screen and not for the other players. I first used a normal script but that removed the coin for every player in the game. This way, it only removes it for the local player. Normal scripts remove it from the server which I don't want.

1 answer

So I did the following and I got the answer:

local db = true
local Coin = game.Workspace.Coin

for i, Coin in pairs(game.Workspace:GetChildren()) do
	if Coin.Name == "Coin" then
		
		Coin.Touched:connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") ~= nil then
				if game.Players:FindFirstChild(hit.Parent.Name) and game.Players[hit.Parent.Name] == game.Players.LocalPlayer then -- Checks if there's a player that touched it and if that player is the LocalPlayer
					if db == true then
						db = false
						Coin.Transparency = 1
						local player = game.Players:GetPlayerFromCharacter(hit.Parent)
						game.ReplicatedStorage.GiveCoins:FireServer(player)
						-- player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
						script.Sound:Play()
						wait(1)
						Coin:Destroy()
						db = true
					end
				end
			end	
		end)
		
	end
end


1 comment

0

where is the script?

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