Why isn't this working?

Hello, I was using this for my simulator and it says leaderstats is nil. I tried using WaitForChild and that still didn’t work, also leaderstats is working.

local ClickedEvent = script.Parent.Events.Clicked

local Player = game.Players.LocalPlayer

local Strength = game.Players.localPlayer.leaderstats.Strength.Value

local function OnClick()

Strength = Strength + 1

end

ClickedEvent.OnServerEvent:Connect(OnClick())

Old Code (before I tried fixing it)

    local Tool = script.Parent

    local EventsFolder = Tool.Events

    local ClickedEvent = EventsFolder.Events.Clicked

    local Player = game.Players.LocalPlayer

    local Strength = Player.leaderstats.Strength.Value

    local function OnClick()

    Strength = Strength + 1

    end

    ClickedEvent.OnServerEvent:Connect(OnClick())

Lots of things wrong with this. Can’t fix all of them quickly so here is a start.

  1. There is no property of the Players service called “localPlayer”–however there is a “LocalPlayer” property. Luau is a case sensitive language! You must do game.Players.LocalPlayer I was wrong about this. Forgot that all properties have a camelCase substitute. Ignore point #1.
  2. Your Strength variable does not store reference to the Value as you think. It is storing reference to the Value of the variable associated with the key Value of Strength, not a reference in memory to Strength–this means you can not assign to Strength.Value by defining the Strength variable because it will just overwrite the value of the Strength variable, not the value of Strength.Value. Instead you must do Strength.Value = Strength.Value + 1 or more concisely, Strength.Value += 1
  3. You are passing the value returned from the OnClick call to OnServerEvent:Connect. What you should be doing is passing reference to the function itself. ClickedEvent.OnServerEvent:Connect(OnClick)
  4. I am unsure if this is a LocalScript or a Server Script. Regardless–your set up is very messed up. I really recommend reading this article as I don’t want to explain the entire process. Bindable Events and Functions | Roblox Creator Documentation
3 Likes

For number 2, I made a variable called Strength that defines the strength value. I added Strength.value so it does have a meaning?

that’s apart of the problem

also, I know how Remote Functions and Events work, that part is working for me. The only part that is not working for me is the variable called Strength

Sorry by Strength.Value I meant:


local Strength = game.Players.LocalPlayer.leaderstats.Strength

local function OnClick()
    Strength.Value += 1
end

I see that I was not clear on that you would need to redefine the Strength variable.

I end up with the same problem.

The issue is leaderstats is being defined as nil

local ClickedEvent = script.Parent.Events.Clicked


local Strength = game.Players.LocalPlayer.leaderstats.Strength.Value

local function OnClick()

Player.leaderstats.Strength.Value = Player.LocalPlayer.leaderstats.Strength.Value + 1

end

ClickedEvent.OnServerEvent:Connect(OnClick)

See issue #4 in my original post. The issue might be because of the type of script you are using.

I am using a normal script, for handling the event

You can not index the LocalPlayer property in a “normal” script because it is nil. It just does not make sense in any way. What player would LocalPlayer even reference if the script runs on the context of the Server? (hint: NONE! Its nonsensical.)

Oh wow, I didn’t realize that, so I would convert it to a local script?

Yes, and since you will be working with a local script I suggest you read the Article I linked. After reading it you should begin to see that your code has more issues!

Yep, thanks a lot, sorry for wasting your time because of my blind eye.

Not a waste of my time. I use the forum to help people! :smiley_cat: