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

Can I get the player who fired a server event?

Hi everyone!

I want to get the player who fired a server event... Is that possible?

local script

local mouse = game.Players.LocalPlayer:GetMouse()
local uis = game:GetService("UserInputService")

mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "1" then
		local main = script.Parent.Parent.Parent.Parent
		
		if main.Visible == true then
			local player = game.Players.LocalPlayer.Name --Gets the player name
			local remote = game.ReplicatedStorage.Events.PurchasePrints --remote
			local char = game.Players.LocalPlayer.Character --Players Char
			local torso = char:WaitForChild("HumanoidRootPart") --HRP
			
			local pos = torso.CFrame --Gets the CFrame of the Plr
			remote:FireServer(pos, player) --Sending The Server the CFrame Pos and the Player name
			print(pos)
			
			local Folder = Instance.new("Folder")
			Folder.Name = game.Players.LocalPlayer.Name -- This makes a new folder to hold CFrame Values of players
			Folder.Parent = game.Workspace.CFrames
			
			local Cframe = Instance.new("CFrameValue")
			Cframe.Name = "CFrameValue" --Making the actual CFrameValue
			Cframe.Parent = Folder
			
			Cframe.Value = pos
			print("Remote Fired Correctly! You did it for once!")
		else
			print("Shop Menu Not Open! Press E To Open!")
		end
	end	
end)

server script

remote = game.ReplicatedStorage.Events.PurchasePrints

remote.OnServerEvent:Connect(function(player)
	if game.Players.LocalPlayer.leaderstats.Money >= 0 then --Checking if the player who fired the event has enough money
		local printer = game.ReplicatedStorage.OtherEnts.MoneyPrinter:Clone()
		printer.Parent = game.Workspace.Ents
		printer.Name = player.Name
		local printerpos = game.Workspace.CFrames:WaitForChild(player.Name) --Gets the Cframe value in the folder and takes the cloned printer and teleports it to the hrp
		local printerposval = printerpos.CFrameValue
		
		printer.CFrame = CFrame.new(printerposval.Value)
		print("Printer Spawned!")
	end
end)

If you have any idea on how to do this, answers would be greatly appreciated! Thank You All!

1 answer

Yes, you can get the player parameter. In your ServerScript, Line 3, you have already got the player who fired to the server.

remote.OnServerEvent:Connect(function(player)

Also, game.Players.LocalPlayer doesn't work in ServerScripts. So, instead you can replace it with your player parameter.

if player.leaderstats.Money >= 0 then --Checking if the player who fired the event has enough money

More info :

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

Lemme know if it helps!

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