I am trying to make a push tool

I am trying to make a push tool, but you can push anywhere. I am trying to make it not be able to push anywhere.

When I added the and it started to stop working.

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

script.Parent.Activated:Connect(function()
if mouse.Target and mouse.Target <= Vector3.new(5) then – everything was working until I added the and
local model = mouse.Target:FindFirstAncestorOfClass(“Model”)
if model then
if model:FindFirstChild(“Humanoid”) then
script.Parent.RemoteEvent:FireServer(model)
end
end
end
end)

1 Like

A couple things I noticed:

  • mouse.Target returns an Instance, not a Vector3 value. If you want to find the position of the mouse, use mouse.Hit, not mouse.Target.
1 Like

Can you use that if you 5 studs away?

You should be checking for the magnitude between the character and the target

instead maybe try:

local root = game.Players.LocalPlayer.Character.PrimaryPart.Position
local mag = (root-mouse.Hit).magnitude
if mouse.Target and mag < 5 then
-- code
end

Output: image

Primary Part came as nil. What are you trying to do with primary part? image

Maybe try something like this instead:

local player = game.Players.LocalPlayer
local char = player.Character
local distAway = 5 -- The amount of studs the player has to be away from somebody to push them
if not char then
return --If the character doesn't exist then we can't get the hum root part
end
local root = char:WaitForChild("HumanoidRootPart")
local mag = (root.Position-mouse.Hit).Magnitude --Distance between two points
if mouse.Target and mag < distAway then
--code
end
3 Likes

The character variable is nil.

image

use player.CharacterAdded:Connect(function(char) or put the localscript inside of StarterCharacterScripts

1 Like

The character is not nil anymore, but this is output image and image

code:
local mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local push = script.Parent:WaitForChild(‘PushAnim’)
local tool = script.Parent
local char = player.Character
local distAway = 5

script.Parent.Activated:Connect(function()
if mouse.Target then
if not char then
return
end
local root = char:WaitForChild(“HumanoidRootPart”)
local mag = (root.Position-mouse.Hit).Magnitude
if mouse.Target and mag < distAway then
local model = mouse.Target:FindFirstAncestorOfClass(“Model”)
if model then
if model:FindFirstChild(“Humanoid”) then
script.Parent.RemoteEvent:FireServer(model)
end
end
end
end
end)

tool.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local humanoid = tool.Parent:FindFirstChildWhichIsA(‘Humanoid’)
local playanim = humanoid:LoadAnimation(push)
playanim:Play()
end)
end)

Wait there is more in output image

image

How would I use vector3 in this line? local mag = (root.Position-mouse.Hit).Magnitude
output image

You should use

local mag = (root.Position-mouse.Hit.Position).Magnitude

instead of

local mag = (root.Position-mouse.Hit).Magnitude
1 Like

my bad i shouldve read clearly lol