Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I push a character away from a part? (Repost, self-answered) [closed]

Asked by
BenSBk 781 Moderation Voter
6 years ago

Somebody recently asked a question about how you could push a character away from a part when the character touches the part. I wrote a detailed answer, but the question was deleted, so I'll post the answer as an answer to my own question instead, in case anybody has the same question.

0
I remember that, I was wondering where that question went. SteamG00B 1633 — 6y
0
ja BenSBk 781 — 6y

Locked by SteamG00B, fredfishy, and Amiaa16

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
2
Answered by
BenSBk 781 Moderation Voter
6 years ago

Let's assume that we have a BasePart named "Part", which is the part that will push Players' characters away.

1local part = workspace.Part

We'll need to connect a function to BasePart.Touched, an RBXScriptSignal that fires whenever a BasePart touches the BasePart, with the other BasePart passed as the only argument.

1local part = workspace.Part
2 
3part.Touched:Connect(function(other_part)
4 
5end)

When part is touched, let's first check if other_part belongs to a character or not with Players:GetPlayerFromCharacter. If not, we'll return from the function. If so, we'll continue and keep the character for later.

01local players = game:GetService("Players")
02local part = workspace.Part
03 
04part.Touched:Connect(function(other_part)
05    -- Check if other_part doesn't belong to a character.
06    local character = other_part.Parent
07    if not players:GetPlayerFromCharacter(character) then
08        return
09    end
10end)

Then, we'll check if the character has a Humanoid or not with Instance:FindFirstChild. If not, we'll return from the function. If so, we'll continue and keep the Humanoid for later.

01local players = game:GetService("Players")
02local part = workspace.Part
03 
04part.Touched:Connect(function(other_part)
05    -- Check if other_part doesn't belong to a character.
06    local character = other_part.Parent
07    if not players:GetPlayerFromCharacter(character) then
08        return
09    end
10    -- Check if character has no Humanoid.
11    local humanoid = character:FindFirstChild("Humanoid")
12    if not humanoid then
13        return
14    end
15end)

Now that we've done the necessary gets and checks, we can push the character away from part. First, we'll sit the Humanoid with Humanoid.Sit = true, otherwise the character won't always be pushed. After that, we'll instantiate CFrame at part.Position facing towards other_part.Position. Think of the CFrame as an arrow pointing away from part and towards other_part. This arrow is the direction that we want the character to be pushed, away from part. To get the Vector3 that describes the direction this arrow is pointing, we can use CFrame.LookVector. Now that we have the desired direction, we'll instantiate BodyForce and assign its BodyForce.Force to the direction multiplied by a multiplier (let's go with 5000). Finally, let's assign its parent to other_part.

01local players = game:GetService("Players")
02local part = workspace.Part
03 
04part.Touched:Connect(function(other_part)
05    -- Check if other_part doesn't belong to a character.
06    local character = other_part.Parent
07    if not players:GetPlayerFromCharacter(character) then
08        return
09    end
10    -- Check if character has no Humanoid.
11    local humanoid = character:FindFirstChild("Humanoid")
12    if not humanoid then
13        return
14    end
15    -- Push other_part away from part.
View all 21 lines...

This works fine, except body_force stays there permanently. Let's Instance:Destroy it after 0.5 seconds.

01local players = game:GetService("Players")
02local part = workspace.Part
03 
04part.Touched:Connect(function(other_part)
05    -- Check if other_part doesn't belong to a character.
06    local character = other_part.Parent
07    if not players:GetPlayerFromCharacter(character) then
08        return
09    end
10    -- Check if character has no Humanoid.
11    local humanoid = character:FindFirstChild("Humanoid")
12    if not humanoid then
13        return
14    end
15    -- Push other_part away from part.
View all 23 lines...

Finally, we should only have one BodyForce per character. Let's create a dictionary with character as keys and true or nil as values (describing whether or not the character has a BodyForce). We'll check and update this appropriately.

01local players = game:GetService("Players")
02local part = workspace.Part
03local character_body_forces = {}
04 
05part.Touched:Connect(function(other_part)
06    -- Check if other_part doesn't belong to a character.
07    local character = other_part.Parent
08    if not players:GetPlayerFromCharacter(character) then
09        return
10    end
11    -- Check if character already has a BodyForce. If not, update.
12    if character_body_forces[character] then
13        return
14    end
15    character_body_forces[character] = true
View all 30 lines...

I hope this helped!

0
I am sure that this will help quite a lot of people because it is an excessive explanation :) SteamG00B 1633 — 6y
Ad