Hello! I wanted to make a survival type game and I decided to start with making axes and trees and such. I’ve never used magnitude before and I wanted to know how I would use it to detect if a player is near a tree they clicked to chop it down with the equipped axe. If anybody could help me out of that or teach me how magnitude works that’ll be great! I’ve already checked the wiki and it kind of didn’t make sense to me, that is why I am asking. Thanks!
Magnitude is obtained when 2 Vector3 values subtract each other. So:
local result = (Vector3.new(5,8,192) - Vector3.new(9,7,5)).Magnitude
I see, so whatever is returned, is that a vector3 value, or studs, or something different?
It will return a number, defining the distance between 2 Vector3 values, in studs.
Alright cool, sorry if I am asking too many questions, but I am bad at math, how do you get a studs value?
Uh, what do your mean by this?
Give your Tree Model a PrimaryPart
local MagnitudeInStuds = (Tree.PrimaryPart.Position - Player.Character.HumanoidRootPart.Position).Magnitude -- Distance between player and the tree's primary part in studs
Basically Subtract 2 Vector3 Values aka Positions with each other in brackets, and put a .Magnitude behind to get the distance between both points.
Okay I’ll try that out, give me a second.
I hope you understand my explanation.
Try AxeTip part instead so it’s like more accurate.
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.
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.
What are you talking about? This category is not for asking full scripts. Please note that that’s a rule on devforum.
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.
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.
No no no, it’s an alternative of while true loop.