Make hammer that flings other players?

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

3 Likes

Is this located in a tool?

30 chars

It’s part of a brick that’s part of a tool, so… Yes?

1 Like

Im not really a good scripter and im not sure if this would do anything but should it be humanoid:Sit()?

1 Like

use something like

if Touch.Parent.Name = [players name] then return end -- you can get this by doing something like script.Parent.Parent.Parent.Name

if you can, can you show me the explorer of the tool so I can assist you more?

1 Like

image

I don’t know if it uploaded correctly just by pasting the image, I have no idea how to use the forum

The animation script is one I made so it plays an animation when clicked
Below it’s all just bricks that are part of the hammer

You can add more a condition at the if Humanoid and Head that is Humanoid ~= tool.Parent.Parent.Character.Humanoid (Assuming it is at starter pack)

I don’t think so, I was testing exactly that and it does work

-- 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.

I forgot about the difference between scripts and local scripts, thanks

1 Like

Tell me if it works or not by pressing the solution button on my post.

When I add the

if Touch.Parent.Name = tool.Parent.Name then return end

It shows "Expected ‘then’ when parsing if statement, got ‘=’ "

1 Like

Use == instead of =
30charslimit

2 Likes

Yes, that was a mistake on my end. Spelling errors.

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?

Where did you put the new line of code?

it should look something like this:

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)

Also, to ensure that it works you might want to add this line below the new line:

if Touch.Parent.Parent.Name == tool.Parent.Name then return end

Mhm, that’s exactly where it is. The only difference is that I changed the velocity to a low value so I can test it without being flung constantly

I’ll try the new line

Yeah, This makes sure if it’s touching a hat, it’ll get it’s second parent of the hat. Which should be the player.