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

I need help when a player touches a part in order to gain five points on the leaderboard?

Asked by 5 years ago
Edited 5 years ago
local enabled = true

script.Parent.Touched:Connect(function(hit)
     local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil and enabled == true then
        local enabled = false
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local points = player.leaderstats.Points
        points.Value = points.Value + 5
        wait(3)
        enabled = true

    end


end)

So recently I'm learning how to code and I stumbled upon an error while coding a touched function. I tried debugging the error, but couldn't simply fix it. So I basically want to give my player five points each time the player touches an object. However, the script keeps on giving me way more than five points per touch. I added a de-bounce, but the error still occurs. Is there a way I can fix this?

0
please make this easier to read ReallyUnikatni 68 — 5y
0
on line 12 try adding script.Parent.Touched:Disconnect() greatneil80 2647 — 5y

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

This is because you're not implementing your debounce correctly

Notice how I removed the local from enabled = false. You were creating a new enabled variable inside this event namespace.

local enabled = true

script.Parent.Touched:Connect(function(hit)
     local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil and enabled == true then
        enabled = false
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local points = player.leaderstats.Points
        points.Value = points.Value + 5
        wait(3)
        enabled = true

    end


end)
Ad

Answer this question