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

help with .Changed?

Asked by
theCJarmy7 1293 Moderation Voter
8 years ago
distance = 10

game.Players.PlayerAdded:connect(function(p)
    while wait() do
mag = (p.Character.Torso.Position - game.Workspace.Part.Position).magnitude
mag.Changed:connect(function()
mag = (p.Character.Torso.Position - game.Workspace.Part.Position).magnitude
    if mag < distance then
        p.PlayerGui.ScreenGui.atm.Visible = true
    elseif mag > distance then
        p.PlayerGui.ScreenGui.atm.Visible = false
    end
end)
    end
end)

this script is supposed to open up a gui when the player gets close enough to it. it works if i take out the .changed, but i dont want it lagging up the game. and another probem with the .Changed, whenever i run the script, it says in the output:

Workspace.mags:6: attempt to index global 'mag' (a number value)

i don't get this error. Is there anyway to do this with .Changed?

0
I'm not 100% sure but I think it works fine without the changed. yoshi8080 445 — 8y

2 answers

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

PreciseLogic is correct. But, instead of detecting when the magnitude changes, why not just check when the Torso is changed?

Torso.Changed:connect(function(Property) -- This has a property of what was changed
    if Property == "Position" then
        print('The position was changed!')
    end
end)

Since I'm preoccupied doing something else, I can't whip up the whole script for you, but the example I gave above will come handy. You can only called the Changed event on Objects, not '.Value's. Hope I helped!

Ad
Log in to vote
0
Answered by 8 years ago

I think the problem is that Magnitude is a value and .Changed can not be used for specific values, it only tracks changes to the Object.

For instance, if I had a Number Value called "Money", I wouldn't do...

Money.Value.Changed:connect(function()
    blah blah
end)

That would result in an error. Instead it would be...

Money.Changed:connect(function()
    blah blah
end)

You'll have to rethink a way on how to find when the magnitude changes.

Answer this question