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

Why does this execute only twice?

char.Torso.Changed:connect(function(prop)
	if prop == "Position" then
		local dis = (block.Position-char.Torso.Position).magnitude
		if dis <= maxDistance then
			gui.Frame.Visible = true
			print"Now you see me"
		else
			gui.Frame.Visible = false
			print"Now you dont"
		end
	end
end)

Within the output it prints "Now you dont" "Now you dont" and then terminates.

EDIT: If it helps, here's the entire script

local player = game.Players.LocalPlayer
if not player.Character then player.CharacterAdded:wait() end
local char = player.Character
block = game.Workspace:WaitForChild("Door")
local maxDistance = 100
local gui = player.PlayerGui.Text
local WaitTime = 0.001
local inProcess = false
local active = false

function open()
	for i=0,22 do
		block.CFrame = block.CFrame + Vector3.new(0,.5,0)
		wait(WaitTime)
	end
	active = true
end

function close()
	for i=0,22 do
		block.CFrame = block.CFrame + Vector3.new(0,-.5,0)
		wait(WaitTime)
	end
	active = false
end

char.Torso.Changed:connect(function(prop)
	if prop == "Position" then
		local dis = (block.Position-char.Torso.Position).magnitude
		if dis <= maxDistance then
			gui.Frame.Visible = true
			print"Now you see me"
		else
			gui.Frame.Visible = false
			print"Now you dont"
		end
	end
end)

game:GetService("UserInputService").InputBegan:connect(function(key,gameThing)
    if key.KeyCode == Enum.KeyCode.Space then--and not gameThing and gui.Visible then
		if inProcess ~= true then
			inProcess = true
			if active ~= true then
				open()
			else
				close()
			end
			inProcess = false
		end
    end
end)

2 answers

✓ Accepted answer

Changed doesn't fire for Physics
If I recall correctly

It's annoying, and it's also incredibly stupid. The only real way you're going to get those sorts of things checked is with a polling loop. Sorry about that. You'll have to look for a different event to check (How about one from the Humanoid?), or use a while loop.

2 comments

0

What I tried was using Humanoid and just purely using the Changed event from that. I was able to get it to work just fine after removing the if statement checking for position as well.

0

Yeah but Changed doesn't fire for Position, for physics updates.

I used MoveDirection under the LocalPlayer's Humanoid. It only has a value when the player is moving.

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