I’ve recently decided to learn how to script, and I tried to make a hammer that flings people when you’re hit by it. While I was making it I found this script to do it, and at least from my (basic) scripting knowledge, it should work.
script.Parent.Touched:connect(function(Touch)
local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
local Head = Touch.Parent.PrimaryPart
if Humanoid and Head then
Humanoid.Sit = true
local vel = Vector3.new(math.random(1, 200), math.random(1, 200), math.random(1, 200))
Head.Velocity = vel
Head.RotVelocity = vel
end
end)
But the thing is, it flings me instead of the person I’m hitting. How can I achieve what I want?
I don’t really want people to write the entire script, I just want a hint or an idea on how to do it, because I’ve been trying to think of a way to accomplish this but I just can’t
-- add this to the top of the script
local tool = script.Parent.Parent.Parent
-- add this right after "script.Parent.Touched:connect(function(Touch)"
if Touch.Parent.Name = tool.Parent.Name then return end
you also might want to use this in a script, not a local script.
Why make it a script and not a local script? Only the client will be able to see it. It won’t happen to other players in the server.
Alright, so I just tried it out, and it does fling the other person but it’s still flinging me.
I anchored the dummy and hit it with the hammer, then I ran into it and it flung me, so I guess it’s flinging me too because of that?
local tool = script.Parent.Parent.Parent
script.Parent.Touched:connect(function(Touch)
if Touch.Parent.Name == tool.Parent.Name then return end -- the new line
local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
local Head = Touch.Parent.PrimaryPart
if Humanoid and Head then
Humanoid.Sit = true
local vel = Vector3.new(math.random(1, 200), math.random(1, 200), math.random(1, 200))
Head.Velocity = vel
Head.RotVelocity = vel
end
end)