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

Script giving me multiple wins if I jump, any way to fix??

Asked by 5 years ago

My script is supposed to detect If the player is touching a part, then give them a win and some points. If the player jumps on the win button, however, they can get up to 5 wins. Is there a way to fix this?

Script:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local checkpoint = script.Parent

function onTouched(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
        local player = Players:GetPlayerFromCharacter(hit.Parent)

        player:LoadCharacter()

        player:WaitForChild("leaderstats").Wins.Value = player:WaitForChild("leaderstats").Wins.Value + 1
        player:WaitForChild("leaderstats").Points.Value = player:WaitForChild("leaderstats").Points.Value + math.random(20,30)

        local checkpointData = ServerStorage:FindFirstChild("CheckpointData")
        if not checkpointData then
            checkpointData = Instance.new("Folder")
            checkpointData.Name = "CheckpointData"
            checkpointData.Parent = ServerStorage
        end

        local userIdString = tostring(player.UserId)
        local checkpointValue = checkpointData:FindFirstChild(userIdString)
        if not checkpointValue then
            checkpointValue = Instance.new("ObjectValue")
            checkpointValue.Name = userIdString
            checkpointValue.Parent = checkpointData

            player.CharacterAdded:connect(function(character)
                wait()
                local storedCheckpoint = ServerStorage.CheckpointData[userIdString].Value
                character:MoveTo(storedCheckpoint.Position + Vector3.new(math.random(-4, 4), 4, math.random(-4, 4)))
            end)
        end

        checkpointValue.Value =  workspace.Start.SpawnLocation
    end

    wait(1)
end

checkpoint.Touched:Connect(onTouched)

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

This answer's to be used as a reference, and assumes you have prior knowledge with Touched

The solution to your problem's to use a debounce. A debounce, to put simply, is a bool that tells the function whether to execute the code or not for a specific amount of time.

Let's take a common example: the Touched

-- Assuming `Part` was already declared.
Part.Touched:Connect(function(Hit)
    print(Hit.Name, 'stepped on me!');
end)

When you step on the part, you'll notice it prints a lot. Maybe 8-10 times. Pretty annoying, right? But, like a said before, there's a solution: a debounce. With the code from before, lets implement the debounce into it.

local Debounce = true;

Part.Touched:Connect(function(Hit)
    if Debounce then
        Debounce = false;
        print(Hit.Name, 'stepped on me!');
        wait(1);
        Debounce = true;
    end
end)

You'll notice that when you step on it, it only prints once after every 1 second. Pretty neat, right? The reason this occurs is because...

  1. The conditional statement (if) is checking if Debounce is truthy (or true). If it is, it's going to execute the following.

  2. Set the Debounce variable to false, so that on the next check it doesn't fire.

  3. It'll print Hit_Name stepped on me!

  4. Waits 1 second

  5. Sets Debounce back to true after the 1 second.

Not sure if there's any other explanation needed lol.

If you have any questions, please don't hesitate to ask me. :) I hope this helped! :D

0
aaaa, i over complicated it thx for the reply. royaltoe 5144 — 5y
0
Would I put my script before the wait(1) NickIsANuke 217 — 5y
0
Yeah, you would. TheeDeathCaster 2368 — 5y
0
Wow, this worked like a charm! Tysm! NickIsANuke 217 — 5y
0
Yw. :> TheeDeathCaster 2368 — 5y
Ad

Answer this question