I’m having difficulty in trying to deal with player to player collision. The idea is to have a player collide with another target player and do something. Edited: I am making a simple game with King of the Hill style to determine if a player should push another.
For example: if player 1 collides with player 2. Player 2 would be affected.
I did the following sample script to test player collision.
-- This is in server script service
local function AddBody(PlayerCharacter)
PlayerCharacter:WaitForChild("HumanoidRootPart").Touched:Connect(function(hit)
local otherPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if otherPlayer then
print(PlayerCharacter.Name.." and "..otherPlayer.Name.." collided!")
end
end)
end
game.Players.PlayerAdded:Connect(function(newPlayer)
newPlayer.CharacterAdded:Connect(AddBody)
end)
Now the issue is that on the output, since all players actually have the function added to them. It registered as either player 1 or 3 as the collision initiator when they do collide with one another.
How does one make it so that if player 1 collide with another player. It’s always player 1 as the initiator and other players as the target? And vice versa to all players in the game.
It depends I guess on what your goal definition of Initiator and Target are. You could do something like checking who’s Velocity magnitude is greater. To get around the issue of the event firing for every client, you could move the code to a central server-side Script, getting the players involved in collision in a similar manner, then checking who’s velocity is greater. The event will still fire twice with PlayerA’s character as the part that touched PlayerB’s character and vice versa but if you do a check on something like the velocity magnitude of each character you can just pass on any event which fires the first of the two Player’s characters as the first argument (since you know the event will also fire with arguments flipped).
The Initiator is the one actively colliding with the Target.
Thanks for the suggestion! So basically by checking Velocity magnitude hmm. Not entirefly familiar but I could give that a try~ Probably have to handle scenarios with 2 players having the same velocity magnitude as well.
I’m not sure I understand moving the code to a central server-side script. Isn’t the script already in server?
Yeah, I’m not sure how “realistic” checking velocity magnitude would feel if two players collide head on. What sort of context is behind this functionality? Are you making a game that’s like a king of the hill style and need to know which player should be pushed?
Just looking back at the code you provided now… Yes you are correct, your code does currently exist in a server-side script. My bad, I was under the impression that the code was in a local script and therefore a copy was created for every player for some reason (Note to self to stop skimming). My point is still valid about the function that would get called for the collisions though. I’m imagining a code structure that looks something like this:
function isInitiator(player1, player2)
--Code here that returns true is player1 is deemed the initiator
--As mentioned that might involve checking if player1's velocity
--magnitude is greater than player2's velocity magnitude
end
function handleCollision(player1, player2)
if isInitiator(player1, player2) then
--code in here will now only run if player1 is deemed Initiator
end
end
For every collision, you would call the handleCollision function, however, because there is a check to see if the first player provided is the initiator, it will simply do nothing if the first player provided is not and when the function is called with arguments swapped (as it will be because collisions are being detected by all players) it will do whatever is to happen.
Another idea for determining Initiator: You could check the rotation of the characters. Whichever character is closer to directly facing the the other would be deemed Initiator.
Ahh yes, my apology. I should edit main post to give more context to readers. In this case, it is as you mentioned. King of the hill style with pushing players on collision.
Thanks for such detailed and comprehensive explanation. Appreciate it. I would give your suggestion a try. I didn’t know you can use if function like that too. Thanks again with this direction.