Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

6 comments

0

The brick no longer does anything on touched. Doesn't give anything in output either.

0

@Draebrewop Is the script running?

0

Yes, inside a part.

0

@DanzLua Nevermind, it was my bad. I accidentally wrote a part of the script wrong.

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?

0

@Draebrewop I don't see a reason for that to happen

This discussion is preserved as it was. New answers, comments, and votes are closed.