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

Confused on how to start with "distancefromcharacter", could someone explain in a simple way to me?

Hello, I am using a script that makes an GUI visible when your cursor hovers over a part. ive been looking for tutorials on youtube and the devforum on how to make it based on distance, but i didnt really understand on how to do it in a script like mine.

Frontdoor is a Part in Workspace and the GUI is invisible in StarterGui, the script is inside the GUI.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

while wait() do
	if Mouse.Target ~=nil and Mouse.Target.Name == 'FrontDoor' then
		script.Parent.Frame.Visible = true
	else
		script.Parent.Frame.Visible = false
	end
end

2 comments

0

Ive tried it but im not really understanding it.

1 answer

✓ Accepted answer

Hello! First, I will instruct you on how functions work.

A function can be relayed multiple arguments, and they can relay arguments back:

function multiplication(x,y) --arguments x, y which can be anything
	return x * y --returns (relays back) a value, which is x times y
end
print(multiplication(10,-1)) --negative ten

Knowing this, the DistanceFromCharacter function only needs one argument, which is the Vector3 coordinate:

print(player:DistanceFromCharacter(Vector3.new(0,0,0))) --distance from the world's center from the player's head

Taking advantage of the fact that functions can relay back results, you can then compare it to see whether it is close enough:

if player:DistanceFromCharacter(mouse.Hit.Position) < 12 --[[twelve studs]] then
	print("Close enough!")
end

If you have any other questions, I could answer them.

1 comment

0

Thank you! Its working.

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