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)
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.