How would I use magnitude to check if a player is near a tree for an axe script

Try AxeTip part instead so it’s like more accurate.

1 Like

put a local script inside the axe and write the following code :

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Up:Connect(function()
    if mouse.Target.Name == "Log" then --change log to what should mouse be on to be activated
        local distancebetweenpos = (player.Character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude
        local distance = 10 --change 10 to required distance to cut
        if distancebetweenpos <= distance then
            local target = mouse.Target.Parent
            game.ReplicatedStorage.CutTree:FireServer(target)
        end
    end
end)

Now a script inside the serverscriptservice

game.ReplicatedStorage.CutTree.OnServerEvent:Connect(function(player, target)
    -- do stuff
end)

I think that’s all

This is not a good way to scrip it, exploiters can already exploit it.

1 Like

Ok so then make a anti exploit

Thanks for that, but that seems really easy to exploit it. I think I know of a way to make it somewhat exploit resistant, so I need to script it. It will take a bit.

1 Like

What are you talking about? This category is not for asking full scripts. Please note that that’s a rule on devforum.

1 Like

Oh sorry i think i forgot the rules ill read them again

Extra tip: use a RunService.Heartbeat event on the server to constantly check the distance between each player and the tree so the script will make sure whoever is the near the tree will have the permission to chop the tree.

1 Like

Won’t be really efficient since I’m going to put a bunch of trees. I’m just going to check the distance from the player to the tree they clicked. And that will be checked on the server so hopefully exploiters cant mess with it.

That will result in inefficiency, instead just check the distance in client and server when they Fire the Chopping Event, and not a Heartbeat Connection.

2 Likes

No no no, it’s an alternative of while true loop.

Yeah I know what heartbeat does lmfao, it runs every frame I’m pretty sure.

1 Like

Oh ok, so whenever they click something, firing the event in this way is efficient?

Well, what I normally would do is checking if it actually something hit the ray (I believe OP uses Raycasting for their weapons), and then if it was a Tree then Fire the event, and run checks for Distance or Hit verification and stuff, which will be efficient.

It’s not a weapon lol just an ordinary axe, I kind of want to make a tree chopping system like the one found in Lumber Tycoon 2

So cast a ray where its origin is played and the ending point is Mouse.Hit.p, then when it hits something specific fire the event?

@astrozzyz If you use .Touched or Region3 For it, its pretty similar to what I suggested, so implementation will be similar.


@ItzMeZeus_IGotHacked That might actually miss a lot of points, using a good module like Raycast Hitbox which is developed for it specifically should be good to check when it hits, and then fire the event.


Anyways, as of your question, magnitude is simply length of a vector, which can be used to get distance between two points as Vector3 always has a .Magnitude property.

The module seems very complicated to use, do you have an advice about this?

Its really not complicated though, there is a piece of example code too, you can check it out. Its pretty easy to use, and provides a lot of features that many might struggle with.

Sorry had to go somewhere yesterday, I tried this and it worked for how I wanted it too, thank you