How To Debug and Tips

Hi there, I keep getting this error
ServerScriptService.LongStickBuy:4: attempt to index nil with 'WaitForChild'
How would I go about debugging this? I obviously can’t use print to see what is going wrong. I’ve been stuck on this problem for about a day now. Also please share your debugging methods/tips.

Thanks.

ServerScript
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local remoteEvent = ReplicatedStorage:WaitForChild(“RemoteEvent1”)
local player = game.Players.LocalPlayer
local playerStats = player:WaitForChild(“leaderstats”)
local playerGold = playerStats:WaitForChild(“Gold”)

local function purchasFunc(player, item)
    local gold = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Gold")

    if playerGold.Value >= 100 and player.Owned.Value == false then 
        playerGold.Value = playerGold.Value - 100 
 
    end
end

remoteEvent.OnServerEvent:Connect(purchasFunc)
1 Like

Hello! This is quite simple. I think that you are sending a RemoteEvent to the server, but with the argument either not defined, or created on the client, so the server just sees nil. Read this article for more info about Client and Server

3 Likes

Paste your code and I will show you what you are doing wrong

How are we supposed to help without the code?

3 Likes

Sorry, I posted it.

This text will be blurred

First thing I’m noticing is that your variable is called “gold” but later in the script you refer to gold as the variable “playerGold” but “playerGold” is never declared anywhere.

1 Like

If it’s a server script why are you getting the local player?

You cant get the localPlayer in a server script like this, this will only work in a localScript

Here again, I think you dont understand the client and server difference that’s why. May I suggest learning about it because if you dont understand, any explanation further will just confuse you

1 Like

And for the error, you are actually waiting for the child leaderstats which doesnt even exists because you cant get the local Player like this


This part of the error tells you in which script the error happened

This number tells you on which line the interpreter stopped

And this is the error