Player = game.Players.LocalPlayer
Character = Player.Character
gui = script.Parent.Text:Clone()
while true do
magnitude = (script.Parent.Position - Character.Torso.Position).magnitude
if gui.Parent == nil and magnitude < 20 then
gui.Parent = game.Players.LocalPlayer.PlayerGui
elseif magnitude >= 20 then
gui.Parent = nil
end
wait(0.5)
end
This script should sense when a player is close enough to a certain object to trigger an event. However, it only works when I delete it and paste it again. After a character dies, the gui doesn't show up until I delete and paste it again. Why does it do this?
EDIT: Hierarchy
Part
Script (this script)
Text (ScreenGui)
Stuff inside Text, irrelevant
EDIT2: New script
Player = game.Players.LocalPlayer
repeat
wait()
until Player.Character
Character = Player.Character
gui = script.Parent.Text:Clone()
while true do
magnitude = (script.Parent.Position - Character.Torso.Position).magnitude
if gui.Parent == nil and magnitude < 20 then
gui.Parent = game.Players.LocalPlayer.PlayerGui
elseif magnitude >= 20 then
gui.Parent = nil
end
print("it just ran")
wait(0.5)
end
The gui still doesn't show up upon respawning until I delete and paste. However, the script does run because "it just ran" is printed in the Output. Why does it skip the if statements?

2 comments
Whats the hierarchy? Use a code block with tabbing to show it.
It worked for me with a trivial adjustment - LocalScripts won't run (online) when not in the Player.