Pushing / flinging a player when they touch a part

Trying to make a player sort of fly backwards when they touch a part, similar to this:

https://gyazo.com/a30ea1537e94355c211e6b950f6a2aa9

I have a basic part touch function setup.

--Variables--
local Brick = script.Parent
--End--

--Code--
local function PlayerTouched(Part)
print("Touched")
	end
end

Brick.Touched:connect(PlayerTouched)
2 Likes

This is the code to fling a player, but first you actually have to get the character. Let me know if you do not know how to do that!

--Variables--
local Brick = script.Parent
--End--

--Code--
local function PlayerTouched(Part)
print("Touched")
	end
end

Brick.Touched:connect(PlayerTouched)

You didn’t change the code, you just typed my code.

Oh sorry I was trying to edit something and messed up here!

Character.HumanoidRootPart.Velocity = Vector3.new(math.random(-1000,1000), 1000,math.random(-1000,1000))

If that doesn’t work, try this

character.PrimaryPart.Velocity = Vector3.new(1000,1000,1000)

Just let me know if it doesn’t work and Ill edit yours to see how I can get it to work and test it in studios. :smiley:

no worries, i’ll try the code now and get the character to make it work

the script isn’t working for me, and I have a variable to get the player and the character so I’m not sure why

To get the character you can check to see if the part is a child of the character.

local function GetCharFromPart(part)
    if part.Parent:FindFirstChild("Humanoid") ~= nil then
        return part.Parent
    end
    if part.Parent.Parent:FindFirstChild("Humanoid") ~= nil then
        return part.Parent.Parent -- In case a hat touches the part.
    end
end

local function PlayerTouched(Part)
    local character = GetCharFromPart(Part)
end

Hm, okay. I’ll just test everything out in studios. I’ll give it to you if I can get it working!

Oh, and if you want to fling the player, you need to set the network owner all character parts to the server.

local function SetCharacterNetworkOwnership(character, owner)
    for _, obj in pairs(character:GetChildren()) do
        if obj:IsA("BasePart") then
            obj:SetNetworkOwner(owner)
        end
    end
end

local function GetCharFromPart(part)
    if part.Parent:FindFirstChild("Humanoid") ~= nil then
        return part.Parent
    end
    if part.Parent.Parent:FindFirstChild("Humanoid") ~= nil then
        return part.Parent.Parent -- In case a hat touches the part.
    end
end

local function PlayerTouched(Part)
    local character = GetCharFromPart(Part)
    
    if character == nil then
        return
    end

    SetCharacterNetworkOwnership(character, nil)

    character.UpperTorso.Velocity = -script.Parent.CFrame.LookVector * 1000 -- Should fling the player in the opposite direction.
end

So all the replies have been giving some things that I’ve never seen but here’s how I personally would go about it.

--# variables
local Brick = script.Parent
local players = {} -- creating a table of all players who touch the part since one touch fires the event a bunch of times
local connection

--# functions
Brick.Touched:Connect(function(TouchedPart)
	if TouchedPart.Parent:FindFirstChildOfClass("Humanoid") then -- making sure it is a humanoid then
		local character = TouchedPart.Parent
		if not character then return end 
		if table.find(players, character) then return end  -- making sure they only touch once

		table.insert(players, character)
		
		local BodyVelocity = Instance.new("BodyVelocity") -- using bodyVelocity to throw the player
		BodyVelocity.Velocity = CFrame.new(TouchedPart.Position,character.PrimaryPart.Position).LookVector * 1000 -- this line makes a force that moves against the part, you can change the 100 to any number to modify the strength of the push
		BodyVelocity.Parent = character.PrimaryPart
		
		-- add your ragdoll script here
		
		wait(0.5) 
		
		BodyVelocity:Destroy() -- you have to destroy it or they might go on forever
		
		-- stop ragdoll here
		
		table.clear(players) -- removing the player so they can get pushed if they touch it again
	end
end)

you might have to modify this bc i didn’t test it but you get the idea, reply if you have questions :slight_smile:

1 Like

A script in a part on the workspace …

local debounce = false
script.Parent.Touched:Connect(function(hit)
	local Hit_Character = hit.Parent:FindFirstChild("Humanoid").Parent or hit.Parent.Parent:FindFirstChild("Humanoid").Parent
	if debounce == false then debounce = true
		local bv = Instance.new("BodyVelocity")
		local offset = Vector3.new()
		bv.Parent = (Hit_Character.LowerTorso)
		bv.MaxForce = Vector3.new(100000,100000,100000) -- can mess with this or the 80's
		bv.Velocity = (Hit_Character.Head.CFrame.LookVector * -80)
			+ (Hit_Character.Head.CFrame.UpVector * 80)
		wait(0.01) bv:Destroy()
		debounce = false
	end	wait(1)
end)

May want to check for Torso vs LowerTorso for the two types of player bodies.

3 Likes

Found this script on here a while back … sadly I can’t remember from who. Changed it just a bit for a better effect.

Bad idea, that’ll create huge times between pressing a movement key and your character actually moving, instead just apply the velocity inside a local script.

1 Like

@2112Jay and @hopeshusbvnd

both of your scripts work, thank you so much! If I could mark 2 answers as solutions, I would.

1 Like

I added my ragdoll script where you told me to but the force isnt applied until they unragdoll

video of what happens: https://gyazo.com/11ac8ba3f87c4728b9153971f1f1db8f

1 Like

hm, strange try ragdolling them before the local BodyVelocity = line