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

help with .Changed?

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?

1 comment

0

I'm not 100% sure but I think it works fine without the changed.

2 answers

✓ Accepted answer

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!

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.

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