I am working on a level system and I’m unsure about 2 things.
1 How to define the LocalPlayer.
2 If I should change this to a local script and where to put it.
The Script:
local player = game.Players:FindFirstChild("LocalPlayer")
-- stats
local LevelStats = player:WaitForChild("LevelStats")
local Level = LevelStats:WaitForChild("Level")
local levelClaim = player:WaitForChild("LevelClaimFolder")
local Claim = levelClaim:WaitForChild("LevelClaim")
local XP = LevelStats:WaitForChild("XP")
-- str values
local neededStr = 500 * Level.Value
-- Function
local function update()
if XP.Value >= neededStr then
Level.Value = Level.Value + 1
Claim.Value = Claim.Value + 1
XP.Value = 0
else
Level.Value = Level.Value
XP.Value = XP.Value
Claim.Value = Claim.Value
end
end
update()
XP.Changed:Connect(function()
update()
end)
Currently, the script is in ServerScriptService, but should I make it client-sided?
You cannot access LocalPlayer on the server, you need to use a PlayerAdded event or have some other means (i.e. leaderstats loads and fires a BindableEvent with the Player)
If you were to move this on the client, creating/changing their stats would not replicate to any other players. You don’t want that.
This script doesn’t make sense, so I assume it’s ChatGPT generated.
First of all, it’s not generated by ChatGPT, I wrote it myself. And secondly, I’ll explain to you how it works so you can kind of understand the system.
local player = game.Players.LocalPlayer -- the player
-- stats
-- the stats below are the players LevelStats (for the Level system)
local LevelStats = player:WaitForChild("LevelStats") -- the folder
local Level = LevelStats:WaitForChild("Level") -- the Level stat itself
local levelClaim = player:WaitForChild("LevelClaimFolder") -- levelClaim folder
local Claim = levelClaim:WaitForChild("LevelClaim") -- level claim stat (used in another system)
local XP = LevelStats:WaitForChild("XP") -- xp stat
-- str values
local neededStr = 500 * Level.Value -- required amount to level up, in this case, it's 500 times whatever the players level is e.g. 500 x 10/11/12/whatever level
-- Function
local function update() -- update function
if XP.Value >= neededStr then -- if the players XP value is more than, or equal to the required amount then...
Level.Value = Level.Value + 1 -- they level up
Claim.Value = Claim.Value + 1 -- their level claim also goes up, which is for another system
XP.Value = 0 -- xp resets due to them leveling up
else -- if the players XP value not more than or equal to then
Level.Value = Level.Value -- stays the same
XP.Value = XP.Value -- stays the same
Claim.Value = Claim.Value -- stays the same
end
end
update() -- instantly runs, although i may put this in a PlayerAdded function
XP.Changed:Connect(function() -- if the XP has changed then it'll run the function
update()
end)
Hope that explanation helps!
EDIT: If you want me to, I can explain the entire system and other systems involved, but only if you deem it necessary.
As @Meta_data said, you cannot simply find a localplayer on a server script, as there are usually multiple players at once.
What you should do is make a “stats” folder inside of the player when they join (PlayerAdded), then check whenever their exp gets changed. To see whose exp got changed, you could then simply check for the value’s parent to get the player instance.
I would recommend keeping it on the server to prevent exploitation of the data IF you plan on saving it & also if like @Meta_data said you want other people to see your level. It would be quite risky for the client to tell the server what level they are.
I’ve got the stats folder, which is the level stats involved with the script, but the purpose of the script above is to check when the LocalPlayer’s XP value gets changed so it Levels them up. Would I move this to a local script, or do I need to make some changes for it to be server sided?
Okay, thanks. But how would I make it so it finds the LocalPlayer’s stats? I understand that I can’t find the LocalPlayer, but the purpose of the script is to check the LocalPlayer’s XP, and if they have the required amount of XP, they can level up. Sorry if I’m causing any confusion.
When a player joins and you make the exp values, so in your playeradded function, you should add a listener to check when their XP gets changed, just like you did before:
XP.Changed:Connect(function() -- if the XP has changed then it'll run the function
update()
end)
Since XP is a child of player, you could do XP.Parent to find the player that had their XP updated.
I have to go, so I’ll reiterate just in case:
you add the .changed listener right after you create the XP value
Players.PlayerAdded:Connect(function(plr)
local XP = Instance.new("NumberValue", plr)
XP.Changed:Connect(function()
local plr = XP.Parent -- we know the player is XP's parent
end)
end)