Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to do local player in remote event?

I am making a script which adds a tool to a players backpack when they click a button, I am using a remote event for this and was wondering how I would make it affect the player who fired the event only. Here is my script:

Local Script


local item = script.Parent
local tool = game.ReplicatedStorage.WoodenSword
local clonedTool = tool:Clone()

item.MouseButton1Click:connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer()
	
	
	
end)


Server Script


local tool = game.ReplicatedStorage.WoodenSword
local clonedTool = tool:Clone()

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
	clonedTool.Parent = game.Players.LocalPlayer.Backpack
end)



	

Thanks!

1 answer

OnServerEvent has a parameter which passes the player who fired the remote event, this can be very useful for a whole variety of things because we don't need to pass the player instance using :FireServer(game.Players.LocalPlayer), instead we can just use the player parameter of OnServerEvent.

I suggest coming to here to learn more on how remote events work.

Remote Events

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
	-- for example if I were to fire this remote event this print would print out my full username, the "Player" instance from "PlayersService" is passed on, not the players character model
	print(player)

	-- you can delete all the code above and replace it with what you're trying to do
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.