Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Access LocalPlayer from a Remote Function Script in the workspace?

Asked by
MexheCo 46
5 years ago

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

Log in to vote
1
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

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.

0
Good Lord, you are the biggest life saver in the history of this website MexheCo 46 — 5y
Ad

Answer this question