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

[SOLVED] leaderstats isn't a valid member of Player?

Asked by 5 years ago
Edited 5 years ago

Hello guys, i'm developing my first game and i'm with a trouble: I've created the Lvl up system in a LocalScript:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

if Player then
    local leaderstats = Instance.new("IntValue", Player)
    leaderstats.Name = "leaderstats"

    local EXP = Instance.new("NumberValue", leaderstats)
    EXP.Name = "EXP"
    EXP.Value = 0
    local Level = Instance.new("NumberValue", leaderstats)
    Level.Name = "Level"

    EXP.Changed:connect(function()
        if EXP.Value >= Level.Value*100 then    
            EXP.Value = EXP.Value - Level.Value*100
            Level.Value = Level.Value+1
        end
    end)
end

And for test I put a Part in the workspace, with this (normal)script:

d = true

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")

    if d == true and humanoid ~= nil then
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local leaderstats = Player:WaitForChild("leaderstats")
        local EXP = leaderstats.EXP

        if d == true then
            d = false
            EXP.Value = EXP.Value+100
            wait(1)
            d = true
        end
    end
end)

But when I touch the part, it says "09:15:09.157 - leaderstats is not a valid member of Player" Why this is happening and how can i fix? I guess that's because i'm trying to change a Local value with a Normal script, but i need these values to be in the Local so i can change it with a Gui latter.

0
localscripts only change things for the client iself; therefor it shouldn't be used to create values inside the player that the server needs to read/write to/from User#20388 0 — 5y
0
But i want to acces and change these values with a Gui latter, I cant properly change with a LocalScript DanielGamerXD 9 — 5y

1 answer

Log in to vote
1
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

Problem

The problem is: You are creating leaderstats with client. not with server, if create by client the server cant detect the leaderstats

Correction

You need to create leaderstats script in ServerScript, not in LocalScript. if is local script only create for the client. not for the server. For this you need to detect a player added, get the player and start a function, for this use: Players.PlayerAdded

What Players.PlayerAdded makes?

  • This simple detect if has added a new player on game, this only works in ServerScript

Example:

--> ServerScript(ServerScriptService)
game.Players.PlayerAdded:Connect(function(plr)
    print("The player " .. plr.Name .. " joined to game!")
end)

Here is fixed script:

--> ServerScript(ServerScriptService)
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
     -- Do not use Instance.new(class, parent). this is deprecated. use local i = Instance.new(class) i.Parent = Player.
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    local EXP = Instance.new("NumberValue")
    EXP.Name = "EXP"
    EXP.Value = 0
    EXP.Parent = leaderstats

    local Level = Instance.new("NumberValue")
    Level.Name = "Level"
    Level.Parent = leaderstats

    -- :connect is deprecated. use :Connect
    EXP.Changed:Connect(function()
        if EXP.Value >= Level.Value*100 then    
            EXP.Value = EXP.Value - Level.Value*100
            Level.Value = Level.Value+1
        end
    end)
end)

Hope it helped, errors? tell-me on comments.

Wiki pages

PlayerAdded


Solved your problems? Accept a answer or put in title [SOLVED]

0
When i change the values with a Gui, the values aparently changes, but they didn't in thw server :/ DanielGamerXD 9 — 5y
0
Use remote events DanielGamerXD StrategicPlayZ 58 — 5y
0
I moved the Instances (EXP and Level) to a LocalScript, now its working properly, i'll search for the remote events to change these values with not only Guis, but with events (touching Parts) in the workspace too, thanks. DanielGamerXD 9 — 5y
0
Use RemoteEvents to change with server. you can create another question and i can help you yHasteeD 1819 — 5y
Ad

Answer this question