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

Why Is It Nil?

Asked by
Vezious 310 Moderation Voter
8 years ago

--Format

ServerScript

PlayerStats(Folder)

Bob(Folder)

Stats(Folder)

Coins(numValue)

-- Server script

function MakeSureCoinsAreNotHacked.OnServerInvoke(PlayerName)
local PlayerFile = script.PlayerStats:FindFirstChild(PlayerName)
local PlayerCoins = PlayerFile.Stats.Coins 
return PlayerCoins.Value

--Local script

local MakeSureCoinsAreNotHacked = MakeSureCoinsAreNotHacked:InvokeServer(game.Players.LocalPlayer.Name)

output - 18:37:53.935 - ServerScriptService.PlayerStatsHandler:40: attempt to index local 'PlayerFile' (a nil value)

1 answer

Log in to vote
1
Answered by
4Bros 550 Moderation Voter
8 years ago

Ok so the first argument on an OnServerInvoke function is always the player who called InvokeServer(). You can then get the name of the player from the first argument rather than passing the player's name along with the player. Also since you used :FindFirstChild() on a userdata value instead of a string, it returned a nil value.

function MakeSureCoinsAreNotHacked.OnServerInvoke(Player) -- Player is always the first argument
local PlayerName = Player.Name -- Get the player's name
local PlayerFile = script.PlayerStats:FindFirstChild(PlayerName)
local PlayerCoins = PlayerFile.Stats.Coins 
return PlayerCoins.Value

local MakeSureCoinsAreNotHacked = MakeSureCoinsAreNotHacked:InvokeServer()


Ad

Answer this question