How do I fire an event for a certain player from another player's script?

Hi there. I’m trying to write code for a pillow that ragdolls whoever you hit. I combined a push tool and a ragdoll script and managed to make yourself ragdoll when you use the pillow, so I know it works fine. However, I have no idea how to fire an event for a certain player coming from another player’s script. I already have a way to detect what player was hit, but I just don’t know how to fire the event for that player only. Any suggestions? (PS sorry if this was already a topic, I just couldn’t find anything).

Thanks,
SpeakerDev

3 Likes

I may be wrong here because I have for some reason never needed to do this but can’t you use a remote event to then fire the other player. I don’t know if it has to go through the server tho because I have never done this.

1 Like

I don’t mean fire a RemoteEvent to then fire another player’s RemoteEvent, I mean is there a way to fire another player’s RemoteEvent from a regular Script? I know there’s this:

RemoteEvent:FireServer()

…but could there be some way to do something like this?

RemoteEvent:FireServer(playerThatWasHit)

…or something like that?

1 Like

I don’t think there is a way to avoid passing the player firing the remote to the server, although you are still able to do:

Remote.OnServerEvent:Connect(function(player1, player2)
	--run code for player2
end)

Also you don’t need to fire the other remote in the first place, you can apply the changes made directly in the script that receives the “request” to access another user remote(which must be a server script anyway).

PS: RemoteEvent:FireServer() can only be used inside client scripts.

1 Like

Will the code put into this function also apply to player1? And since I have never worked with something like this before, do I define player2, or does it do it on its own?

1 Like

player1 is passed in the RemoteEvent by default, so player2 is the first argument that’s manually passed to the RemoteEvent

--Player1 client script
RemoteEvent:FireServer(player2)

I’ve defined player2 and I put it into the event inside player1’s client script. Is this how I’m supposed to do it?

local model = mouse.Target.Parent
local player2 = model.Name

...

script.Parent.Push:FireServer(model, player2)

player2 is an StringValue not a Instance, you just have to do :FireServer(model).

Alright, I have pieced together this:

script.Parent.Push.OnServerEvent:Connect(function(player1, player2)
	Trigger:FireClient()
end)

Will this work?

Trigger:FireClient(player2)

I tested and got this error:

10:19:42.735  FireClient: player argument must be a Player object  -  Server - Script:25

…the error occurring here:

script.Parent.Push.OnServerEvent:Connect(function(player1, player2)
	**Trigger:FireClient(player2)**
end)

How did you define the player2 object? That might be why

Well, pass through a player object; That’s it.

The argument must be defined as a player object

Names don’t matter in programming languages for variables