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

Why does this execute only twice?

Asked by 8 years ago
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

Log in to vote
0
Answered by 8 years ago

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.

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. M39a9am3R 3210 — 8y
0
Yeah but Changed doesn't fire for Position, for physics updates. User#6546 35 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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

Answer this question