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

My coin giver on touch script gives coins to everyone rather than the person who touched it, help?

local brick = script.Parent
 
function onTouched(hit)
    for i,player in ipairs(game.Players:GetPlayers()) do
        if player.Character then
            local stat = player:FindFirstChild("leaderstats")
            if stat then
                player.leaderstats.Coins.Value = player.leaderstats.Coins.Value +50
                brick:Destroy()
            end
        end
    end
end
 
 
brick.Touched:connect(onTouched)

I used this script and put it inside the part that gives coins, when the player touches it, it disappears and gives 50 coins, only problem is that it gives 50 coins to everyone.

1 comment

0

NICCO is right on line 4 you did game.Player:GetPlayers()) which will get all players not just the one you want

1 answer

I'm not really good with ipairs, so I can't really explain but instead try this:

local brick = script.Parent

function onTouched(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	if hum then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		local stat = plr:FindFirstChild("leaderstats")
		if stat then
			stat:FindFirstChild("Coins").Value = stat:FindFirstChild("Coins").Value + 50
			brick:Destroy()
		end
	end
end

This should work, in your code you are getting all the players that are in the Player Service and not specifying which player, you could've also just added..

if plr.Name == hit.Parent.Name then 
	--do stuff
end

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