Let's assume that we have a BasePart named "Part", which is the part that will push Players' characters away.
1 | local 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.
1 | local part = workspace.Part |
3 | part.Touched:Connect( function (other_part) |
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.
01 | local players = game:GetService( "Players" ) |
02 | local part = workspace.Part |
04 | part.Touched:Connect( function (other_part) |
06 | local character = other_part.Parent |
07 | if not players:GetPlayerFromCharacter(character) then |
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.
01 | local players = game:GetService( "Players" ) |
02 | local part = workspace.Part |
04 | part.Touched:Connect( function (other_part) |
06 | local character = other_part.Parent |
07 | if not players:GetPlayerFromCharacter(character) then |
11 | local humanoid = character:FindFirstChild( "Humanoid" ) |
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.
01 | local players = game:GetService( "Players" ) |
02 | local part = workspace.Part |
04 | part.Touched:Connect( function (other_part) |
06 | local character = other_part.Parent |
07 | if not players:GetPlayerFromCharacter(character) then |
11 | local humanoid = character:FindFirstChild( "Humanoid" ) |
17 | local direction = CFrame.new(part.Position, other_part.Position).LookVector |
18 | local body_force = Instance.new( "BodyForce" ) |
19 | body_force.Force = direction * 5000 |
20 | body_force.Parent = other_part |
This works fine, except body_force stays there permanently. Let's Instance:Destroy
it after 0.5 seconds.
01 | local players = game:GetService( "Players" ) |
02 | local part = workspace.Part |
04 | part.Touched:Connect( function (other_part) |
06 | local character = other_part.Parent |
07 | if not players:GetPlayerFromCharacter(character) then |
11 | local humanoid = character:FindFirstChild( "Humanoid" ) |
17 | local direction = CFrame.new(part.Position, other_part.Position).LookVector |
18 | local body_force = Instance.new( "BodyForce" ) |
19 | body_force.Force = direction * 5000 |
20 | body_force.Parent = other_part |
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.
01 | local players = game:GetService( "Players" ) |
02 | local part = workspace.Part |
03 | local character_body_forces = { } |
05 | part.Touched:Connect( function (other_part) |
07 | local character = other_part.Parent |
08 | if not players:GetPlayerFromCharacter(character) then |
12 | if character_body_forces [ character ] then |
15 | character_body_forces [ character ] = true |
17 | local humanoid = character:FindFirstChild( "Humanoid" ) |
23 | local direction = CFrame.new(part.Position, other_part.Position).LookVector |
24 | local body_force = Instance.new( "BodyForce" ) |
25 | body_force.Force = direction * 5000 |
26 | body_force.Parent = other_part |
29 | character_body_forces [ character ] = nil |
I hope this helped!
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?