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

Access LocalPlayer from a Remote Function Script in the workspace?

I need to access the LocalPlayer from a Script in the workspace. Is there any way to do this? On any code I try, I always get a return of nil when trying to access LocalPlayer.

Note that everything works fine besides accessing the LocalPlayer

My (attempted) RemoteEvent Script code:

ReplicatedStorage = game:GetService("ReplicatedStorage")
createPartEvent = Instance.new("RemoteEvent", ReplicatedStorage)
createPartEvent.Name = "HotDog"
player = game.Players.LocalPlayer
 
local function onCreatePartFired(player)
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1
end
 
createPartEvent.OnServerEvent:Connect(onCreatePartFired)

The LocalScript that calls the RemoteEvent Script:

function Click()

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local createPartEvent = ReplicatedStorage:WaitForChild("HotDog")
 
createPartEvent:FireServer()
print ("Fired Successfully")
end


script.Parent.MouseButton1Click:connect(Click)

1 answer

✓ Accepted answer

You don't need to define LocalPlayer in the server script. It's automatically passed as the 1st argument of OnServerEvent.

Simply remove the line 4 of your server script and you should be good.

1 comment

0

Good Lord, you are the biggest life saver in the history of this website

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