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

How can I get the player from a server script?

I have a localscript that fires a remoteevent when a textbutton is clicked, the event welds a meshpart to the right hand from the server script. the problem is that i dont have a variable for the player. i accepted the callback from the remote event and it gives me this error. 15:41:41.583 - Players.Ruunic.PlayerGui.Inventory.Rock.Script:24: attempt to call field 'OnServerEvent' (a userdata value) keep in mind that i need the player value as a global variable, as i use it in a mouse.Button1Down function later.

-- Server
	ev2.OnServerEvent(p):connect(function()
		local hc = item:FindFirstChildWhichIsA('MeshPart')
		local c = p.Character
		mouse = p:GetMouse()
		if not equipped then
			equipped = true
			item.Parent = workspace
			weld(hc,c.RightHand)
		elseif equipped then
			equipped = false
			hc.WeldConstraint:Destroy()
			item.Parent = p.PlayerGui.Inventory
			c.RightHand.WeldConstraint:Destroy()
		end
	end)
--Client
local p = game.Players.LocalPlayer
local item = p.PlayerGui.Inventory.Rock
script.Parent.Text = item.Name
local c = p.Character
local mouse = p:GetMouse()
--local hAnim = game.ReplicatedStorage.Animations.Slash
local equipped = false
local iName = item.Name
local ev = game.ReplicatedStorage.Events.Equip
local drop = game.ReplicatedStorage.Events.Drop

--game.ReplicatedStorage.Events.GetPlayer:FireServer(p)

function drop()
	drop:FireServer()
	--[[item:FindFirstChildOfClass('MeshPart').Position = c.UpperTorso.Position + Vector3.new(0,4,0)
	item.Parent = workspace]]
end
script.Parent.MouseButton1Click:connect(function()
	ev:FireServer(p)
end)

if item.droppable.Value == true then
	script.Parent.MouseButton2Click:connect(drop)
else
	script.Parent.Parent.Parent.drop.Visible = true
	wait(1.5)
	script.Parent.Parent.Parent.drop.Visible = false
end

my code is sloppy, i know. i just gotta get this one thing done to finish my game lol

2 answers

On line 2 replace ev2.OnServerEvent(p):connect(function() with ev2.OnServerEvent:connect(function(p)

RemoteEvents

There is a first value that you have to name for the player who activates the RemoteEvent... An example would be in your code, where it says function(), put a plr there and that will be the player who activates the RemoteEvent.

Code Example

--Made By MillerrIAm
--------Variables-------
--------Main Code------
plr.PlayerAdded:Connect(function(player)
end)

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