Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Adding points on touch?

Asked by 8 years ago

So I have a brick where you step on it and it rewards points. The problem is that if I set it to add 10 points, it adds 10 points extremely rapidly. I can't seem to get to add 10 points and then that's it.

Here is the code:

amnt = 10
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("Points")
                if (score~=nil) then
                    score.Value = score.Value + amnt
                end
            end
        end
    end
end

script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
1
Answered by 8 years ago

The following code will add 10 points to the player's score, and will not do so for the same player when they touch the brick again. Keep in mind that if the player leaves the server and rejoins the same one, then they will not be able to receive points from the part anymore. To combat this, you can use the PlayerRemoving event and remove the player from the table, but I'll leave that up to you.

local amnt = 10 --the amount of points the player will receive
local tab = {} --create a table. This will serve as a blacklist to players who already received points.

script.Parent.Touched:connect(function(part)--run the following code when the script's parent is touched
    local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
    if tab[part.Parent.Name] or not player then return end --terminate the code if the player is blacklisted or does not exist
    tab[player.Name] = true --blacklist the player
    local stats = player:FindFirstChild("leaderstats") --define stats
    local score = stats:FindFirstChild("Points") --define score
    if not stats or not score then return end --if neither exist, then terminate the code
    score.Value = score.Value + amnt --give the player points
end)
Ad

Answer this question