So I have had an issue with values not saving to a leaderboard because it was a local script so here I have a server script I made but I get the following error:
Script:3: attempt to index local 'user' (a nil value)
So the problem is, how do I get the player who clicked the text button in a server script?
local function onClicked(player) local user = game.Players:GetPlayerFromCharacter(player) local stats = user:findFirstChild("leaderstats") if stats ~= nil then local cash = stats:findFirstChild("Money") cash.Value = cash.Value + 500 else print("Error1") end end script.Parent.MouseButton1Click:connect(onClicked)
This script is supposed to be a LocalScript (read this guide; servers cannot handle user input directly when playing online). You have up to 2 other problems:
RemoteFunction(see tutorial) to communicate to the server what it wants changed. (This is for FilteringEnabled places [aka non-Experimental Mode].)
ie, change line 2 to be
local user = game.Players.LocalPlayer, make it a LocalScript, and turn on experimental mode (aka turn off FilteringEnabled) if needed and it should work.
Only client side items can grab localplayer. Use an argument, like ontouched for example. Its arg is the part that touched it. Same applies to click-detectors.
To access the local player from a script, you could use something like this:
local players = game.Players wait(1) local camera = workspace.Camera local player = nil for i, v in pairs(players:GetPlayers()) do if v.Name ~= camera.CameraSubject.Parent.Name then player = v end end
This will only work if the camera subject is the character, though.