Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 2 years ago

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
0
Ive tried it but im not really understanding it. NEMOTASTlC 7 — 2y

1 answer

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
2 years ago

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.

0
Thank you! Its working. NEMOTASTlC 7 — 2y
Ad

Answer this question