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

How do I fix this problem with a part being touched?

Asked by 6 years ago

I am attempting to create a part that when touched gives players points, which it does, but gives them more than intended and even with a debounce is very glitchy. Also I am trying to make it so everyone who touches it gets only a single coin and win, but the debounce for the first player to touch doesn't allow this. This is what I have so far.

local brick = script.Parent
local buttonPressed = false

function onTouched(hit)
    if not buttonPressed then
        local buttonPressed = true
          if hit.Parent:FindFirstChild("Humanoid")~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)~=nil then
            local player=game.Players:GetPlayerFromCharacter(hit.Parent)
                if player.Character then

            local stat = player:FindFirstChild("leaderstats")
            if stat then
                player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
                player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
                hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-40, 1.5, 64)
                wait(10)
                buttonPressed = true            
            end
        end
    end
end
    end



brick.Touched:connect(onTouched)

1 answer

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

The reason your debounce isn't working is because you're using it incorrectly, firstly the parts where you change the debounce should be on the outer skirts of the function, you are also redefining the debounce locally inside the function First let's remove unnecessary if statements.

local brick = script.Parent
local buttonPressed = false

function onTouched(hit)
    if buttonPressed == false then
        buttonPressed = true
        if hit.Parent:FindFirstChild("Humanoid")~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)~=nil then
            local player=game.Players:GetPlayerFromCharacter(hit.Parent)
            if player.Character then
                local stat = player:FindFirstChild("leaderstats")
                if stat then
                    player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
                    player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
                    hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-40, 1.5, 64)
                    wait(10)    
                end
            end
        end
        buttonPressed = false
    end
end
brick.Touched:connect(onTouched)

Now the debounce should be working properly

Im going to combine your function and where you call it together now

local brick = script.Parent
local buttonPressed = false

brick.Touched:connect(function(hit)
    if buttonPressed == false then
        buttonPressed = true
        if hit.Parent:FindFirstChild("Humanoid")~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)~=nil then
            local player=game.Players:GetPlayerFromCharacter(hit.Parent)
            if player.Character then
                local stat = player:FindFirstChild("leaderstats")
                if stat then
                    player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
                    player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
                    hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-40, 1.5, 64)
                    wait(10)    
                end
            end
        end
        buttonPressed = false
    end
end)
0
The brick no longer does anything on touched. Doesn't give anything in output either. Draebrewop 114 — 6y
0
@Draebrewop Is the script running? DanzLua 2879 — 6y
0
Yes, inside a part. Draebrewop 114 — 6y
0
@DanzLua Nevermind, it was my bad. I accidentally wrote a part of the script wrong. Draebrewop 114 — 6y
View all comments (2 more)
0
However, for some reason it takes a around 5 seconds to give the points as well as teleport the player to the position, which happens even if I change the wait time? Draebrewop 114 — 6y
0
@Draebrewop I don't see a reason for that to happen DanzLua 2879 — 6y
Ad

Answer this question