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

How come my giver brick script gives points to everyone and not the one who touched?

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.Points.Value = player.leaderstats.Points.Value +20
                brick:Destroy()
            end
        end
    end
end
 
 
brick.Touched:connect(onTouched)

I made this script and its supposed to add 20 points to whoever touches it, but instead it gives everyone 20 :/ help?

1 comment

0

I forgot an end in my script. Should work fine now. Cheers!

1 answer

✓ Accepted answer

Hello, ThatOneKid7590.

Your script currently gives everyone in the game 20 points since you have used a for loop that iterates through all the players in the Players folder. You want to give points to the localplayer, not all players. In your case, the for loop is completely useless..

Lets restart from the beginning

local brick = script.Parent--Defining our variable, easy.

function onTouched(hit)

end

brick.Touched:connect(onTouched) --Firing our event when the brick is touchedé

It is to be noted that the Hit argument I've used represents the part that touched it. In our case, it could be your characters right leg.

Now, we will make sure that whatever touched it is indeed a player, and not a brick.

if(hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent)) then --If the character has a humanoid and is a player, then

--hit would be the part that was hit, therefore being head,legs,etc. The parent of those parts is the character.Thus why I used hit.Parent

end

After that, we want to award the player 20 points. The player being hit.Parent.

local player = game.Players:GetPlayerFromCharacter(hit.Parent) --GetPlayerFromCharacter basically associates the Character( ex:Legs parent) to the Player.

local leaderstats = player:FindFirstChild("leaderstats")

if leaderstats then
	leaderstats.Points.Value = leaderstats.Points.Value +20
end

brick:Destroy()

The final code should look like this:

EDIT: I forgot to add an end in my final code. Sorry for the inconvenience. Should work now

local brick = script.Parent--Defining our variable, easy.

function onTouched(hit)
	if(hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent)) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --GetPlayerFromCharacter basically associates the Character( ex:Legs parent) to the Player.
		local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
    leaderstats.Points.Value = leaderstats.Points.Value +20
end

brick:Destroy()
	end

end

brick.Touched:connect(onTouched) --Firing our event when the brick is touchedé

Cheers!

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