✓ Accepted answer
The touched event fires several times when a player touches an object. It fires each time a different body part touches the brick and it may fire multiple times per body part depending on the player's movement on the part. To prevent giving a player multiple points, add the player name to a table after the point has been awarded and check to see if the player's name is in the table before awarding the point.
Additionally, your comparison to 1 in this statement if (score ~= 1)then will always be true because an IntValue is never equal to a number. The correct comparison is if (score.Value ~= 1) then
This code assumes a Script rather than a LocalScript, because the pointAwarded table would be recreated each time a player respawns if it is in a LocalScript. That wuld allow the player to collect the point again.
local pointAwarded = {}
function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:findFirstChild("leaderstats")
if (stats~=nil) then
local score = stats:findFirstChild("Level")
if (score.Value ~= 1) then
if not pointAwarded[h.Parent.Name] then
pointAwarded[h.Parent.Name] = true
score.Value = score.Value + 1
end
end
end
end
end
end
script.Parent.Touched:connect(onTouched)
1 comment
the score default is 1 so the script has to say ' if (score.Value ~= 2) then' now it should work!