How to make a part do function when player is about 10 or less studs from it?
Here is my code
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Wait()
local MagnitudeInStuds = (script.Parent.Position - Player.Character.HumanoidRootPart.Position).Magnitude
if Player:DistanceFromCharacter(script.Parent.Position) <=10 then
script.Parent.Transparency = 1
end
end)
i think its full of mistakes i really need support with magnitude
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Wait()
task.spawn(function()
while true and task.wait() do
local MagnitudeInStuds = (script.Parent.Position - Player.Character.HumanoidRootPart.Position).Magnitude
if (MagnitudeInStuds <= 10) then
script.Parent.Transparency = 1
else
script.Parent.Transparency = 0
end
end
end)
end)
creates a new thread so code can continue running after the loop
example:
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Wait()
while true and task.wait() do
local MagnitudeInStuds = (script.Parent.Position - Player.Character.HumanoidRootPart.Position).Magnitude
if (MagnitudeInStuds <= 10) then
script.Parent.Transparency = 1
else
script.Parent.Transparency = 0
end
end
print("hi") --// this will not run because of the while true loop above, create a new thread with task.spawn to avoid this.
end)