Script to check distance, what's wrong with it?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I have a script that should make the mesh tornado transparent when you are close, but it does not do that.

  1. What is the issue? Include screenshots / videos if possible!

The Script:

local humanoidRootPart = game.Players.LocalPlayer.HumanoidRootPart
local modelPrimaryPart = workspace.Tornado.PrimaryPart
local meshTornado = workspace.Tornado.Tornado.Main.distrender

local function GetDistance(p1,p2)
	local Distance = (p1.Position - p2.Position).magnitude
	return Distance
end

while true do
	task.wait(1)
	if GetDistance(modelPrimaryPart, humanoidRootPart) > 500 then
		meshTornado["distrenderTor 1"].Transparency = 0
		meshTornado["distrenderTor 2"].Transparency = 0.5
		meshTornado["disrenderTor 3"].Transparency = 0.7
		
	elseif GetDistance(modelPrimaryPart, humanoidRootPart) < 500 then
		meshTornado["distrenderTor 1"].Transparency = 1
		meshTornado["distrenderTor 2"].Transparency = 1
		meshTornado["disrenderTor 3"].Transparency = 1
	end
end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried rewriting it a few times, but this script was originally a solution to someone else’s topic and I just changed the function.

Try printing inside of the if statements to see if it detects >500 or <500.

Works fine for me

local RunService = game:GetService("RunService");
local Player = game:GetService("Players").LocalPlayer;

local playerChar = Player.Character or Player.CharacterAdded:Wait();
local playerHRP = playerChar:WaitForChild("HumanoidRootPart");
local meshTornado = game.Workspace:WaitForChild("Part");

local function GetDistance(p1,p2)
	local Distance = (p1.Position - p2.Position).magnitude
	return Distance
end

RunService.Heartbeat:Connect(function()
	local distance = GetDistance(meshTornado, playerHRP);
	if distance > 16 then
		print("hidden")
		meshTornado.Transparency = 0;
	end
	if distance < 16 then
		print("visible")
		meshTornado.Transparency = 1;
	end
end)

Try this code out if it works. I changed the variables a bit so I can test it myself, so just change it back to fit your code.

1 Like

Im pretty sure the M is supposed to be capitalized in Magnitude.

Try this:

local Distance = (p1.Position - p2.Position).Magnitude
1 Like

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