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)
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
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
The character is not nil anymore, but this is output and
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)