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

Why isn't this script changing the variable?

hit = true
game.Workspace.ThunderChange.Touched:connect(function(hit)

	if hit == true then
		print("Hit equals 1")
		repeat hit = false until hit == false
	elseif hit == false then
		print("Hit equals 2")
		repeat hit = true until hit == true
	end

end)

A simple script I made that detects when an object is touched. I want to make it change a sound from true to false or false to true using one block. It won't print to output or even work. Anyone understand why this won't work. Also when I hit the block when it did work, with would deploy like 1000x. How can I keep that from happening?

1 answer

✓ Accepted answer

Improvements

Use WaitForChild() to check if an instance exists before using it

Use workspace instead of game.Workspace

Use Connect instead of connect [Deprecation]

Issues

You have a variable in the beginning that is then overriden by the one you detect in the touched event.

The "hit" parameter refers to the instance that touched "ThunderChange"

Revised ServerScript

local bool = true

workspace:WaitForChild("ThunderChange").Touched:Connect(function(hit)
	if bool == true then
		print("Bool equals true")
		bool = false
	elseif bool == false then
		print("Bool equals false")
		bool = true
	end
end)

2 comments

0

Thank you! It triggers really fast still but at least it works!

0

np, you can add a debounce and wait if you need to to reduce the speed with which the event fires

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