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

How can I get this currency pickup to work?

I am trying to make a currency pickup system.

When I were to touch the object, it should increase the leaderboard value for the player's coins via leaderstats, but it doesn't. I did not receive any error messages, so I am completely left in the dark.

The following code is in a local script.

coin = script.Parent

coin.Touched:Connect(function(player)
	if coin.AlreadyCollected.Value == false then
		local LocalPlayer = game.Players.LocalPlayer
		local gold = LocalPlayer.leaderstats.Gold.Value
		
		gold = gold + 1
	end
end)

Any help will be appreciated! :)

2 answers

✓ Accepted answer

Just a heads up that LocalScripts only affect that one person, or the client! I'll attempt to remake your script here;

--Script
local coin = script.Parent

coin.Touched:Connect(function(hit) --.Touched only returns the part that touched it, not the player
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Checks if the hit's parent is a character
	if plr and not coin.AlreadyCollected.Value then --If it is, and if the bool is false then
		coin.AlreadyCollected.Value = true --Makes it so it can't be collected again
		local gold = plr.leaderstats.Gold.Value
		gold = gold + 1 --Adds the gold
	end
end)

Localscripts only affect the client, and don't even work in the workspace. Get the character using game.Players:GetPlayerFromCharacter(player.Parent), and then add it to the player's currency through that.

1 comment

0

"player" comes from your touched object btw

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