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

script activates more than once ontouch?

Asked by
Roytt 64
8 years ago

I have this script that is suposed to increase a value by one if a player touches the part it and decrease it by one if that same player touches the part again but sometimes it activates more than once per touch rapidly adding and substracting numbers:

local b =false
script.Parent.Touched:connect(function(move)
    if b == false and move.Parent:FindFirstChild("Torso") then
        b = true
        if not move.Parent.Torso:FindFirstChild("num") then
            local c = Instance.new("BoolValue")
            c.Name = "num"
            c.Value = true
            c.Parent = move.Parent.Torso
            script.Parent.Value.Value = script.Parent.Value.Value + 1
            wait()
        elseif move.Parent.Torso:FindFirstChild("num") and move.Parent.Torso.num.Value == true then
            move.Parent.Torso.num.Value = false
            script.Parent.Value.Value = script.Parent.Value.Value - 1
            wait()
        elseif move.Parent.Torso:FindFirstChild("num") and move.Parent.Torso.num.Value == false then
            move.Parent.Torso.num.Value = true
            script.Parent.Value.Value = script.Parent.Value.Value + 1
            wait()
        end
        wait()
    end
    wait()
    b = false
end)

2 answers

Log in to vote
1
Answered by 8 years ago

The debounce value is outside the first if statement, so it is set to false every time the part is touched.

local b = false
script.Parent.Touched:connect(function(move)
    if b == false and move.Parent:FindFirstChild("Torso") then
        b = true
        if not move.Parent.Torso:FindFirstChild("num") then
            local c = Instance.new("BoolValue")
            c.Name = "num"
            c.Value = true
            c.Parent = move.Parent.Torso
            script.Parent.Value.Value = script.Parent.Value.Value + 1
        elseif move.Parent.Torso:FindFirstChild("num") and move.Parent.Torso.num.Value == true then
            move.Parent.Torso.num.Value = false
            script.Parent.Value.Value = script.Parent.Value.Value - 1
        elseif move.Parent.Torso:FindFirstChild("num") and move.Parent.Torso.num.Value == false then
            move.Parent.Torso.num.Value = true
            script.Parent.Value.Value = script.Parent.Value.Value + 1
        end
    wait(1) --single wait
        b = false
    end
end)

0
is there a way to make it work without adding wait(1)? it seems like it can't produce problems if two players touch the part almost at the same time Roytt 64 — 8y
0
Maybe. Is the problem that the value will increase/decrease two times in a row? If so this is because the bool values in the characters are different. You could make it so that touching the part toggles a single bool value instead individual ones per character, but in your question you say " if that same player touches the part again". Does this mean unique values for each character? BlueImagination 45 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

I am pretty sure the reason for that is because your wait time is way too little... Also for a cool down I suggest using a Debounce it's simple and it's more efficient Or so I think so....

Answer this question