BoolValue not changing

So I want the BoolValue to change when the player touches a part, and that part works fine, when I print it it says true, but when another scripts checks the value, it always comes out as false.

HasTouched = model.Part:WaitForChild("HasTouched").Value

Button.Touched:Connect(function()
	if deb == false and HasTouched == false then
		deb = true
		print(HasTouched)
	end
end)

So what am I doing wrong?

Is your script a local script? It needs to be a serverscript. Server scripts can’t see changes made by clients. If you need it to be done via a local script, fire an event and change it on the server.

You’re only retrieving the value once. You should either put HasTouched in the Touched event’s hooked function or reference the BoolValue, then use HasTouched.Value == false or not HasTouched.Value in the condition.

Try this:

Has touched = model.Part:FindFirstChild("HasTouched")

Button.Touched:Connect(function()
    if deb == false and HasTouched.Value == false then
        deb = true
        print(HasTouched)
    end
end)

You never set BoolValue to true you only set debounce variable to true.

1 Like

This:

If you print out HasTouched, it just prints “true”. HasTouched doesn’t refer to the BoolValue instance you want to link it to, HasTouched is just a local true or false variable here.

HasTouched should refer to the BoolValue instance instead.

local HasTouched = model.Part:WaitForChild("HasTouched")


anEvent:Connect(function()
    HasTouched.Value = not HasTouched.Value
end)

Summary, the HasTouched in your script is just a local variable and not the BoolValue. Your script just sets the local variable’s value to the BoolValue’s current value. Hope this helped!

1 Like

You do not need a BoolValue for this, simple create a variable:

local Debounce = false

Then, we can implement a debounce system:

local Debounce = false
local DebounceTime = 1 -- In seconds.
local Debounces = {} -- Our debounce dictionary

local Players = game:GetService('Players')
local Workspace = game:GetService('Workspace')
local TouchTrigger = Workspace.Part

TouchTrigger.Touched:Connect(function(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if Player then
		if Debounces[Player] then 
			-- Accessing the player instance is faster than accessing its properties, ex Player.Name.
			return
		end
		Debounces[Player] = true
		
		-- Do whatever you'd like to.
		
		wait(DebounceTime)
		Debounces[Player] = nil -- There's no need to keep this in the memory, so we're setting it to nil instead of false.
	end
end)

This is a player-based debounce.
Just some pseudo code though – feel free to edit.

1 Like

Well, I didn’t show the entire script, only the things necessary for this thread, but I do need a BoolValue, luckily @g_captain helped me spot the issue.
Thanks anyway.