Magnitude support

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

1 Like

Try using CFrame rather than Position

1 Like

something like this?

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)
2 Likes

It failed and says Unable to cast CoordinateFrame to Vector3

1 Like

Hm, oops, Try now CFrame.Position

2 Likes

thats the same thing as doing .Position

2 Likes

Big thanks the script works very well but may i ask you to tell me what does task.spawn do

1 Like

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)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.