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.
The problem is: You are creating leaderstats with client. not with server, if create by client the server cant detect the leaderstats
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?
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)