When creating a Leveling script you have to use the PlayerAdded
Event to add the certain Int Values. Such as XP,XPCap and Level
. When using the PlayerAdded in your script you are unable to say to change Those XP,XPCap and Level
Values on the client side. must do so in the Server.
So then how can I fire this to the Server? But at the same time keep the code inside the PlayerAdded function. Or is there a better way? I don't have my script on me but i'll try to write one. Here's my attempt.(won't be perfect)
Server Script
--Variables-- game.Players.PlayerAdded:Connect(function() Level = Instance.new("IntValue") Level.Parent = Player XP = Instance.new("IntValue") XP.Parent = Player XPCap = Instance.new("IntValue") XPCap.Parent = Player while true do wait() if XP <= XPCap then Level.Value = Level.Value + 1 end end end) --I'm on my phone so i can't write a full script but show you what i mean--
Hopefully this makes sense
To change the values of anything on the server through the client we must implement remote events/functions. You can find Roblox's article on the topic here:
Roblox Developer Hub Page: https://wiki.roblox.com/index.php?title=Remote_Functions_%26_Events
To answer this simply, edit your Script with the code below and add a RemoteFunction in your replicated storage with any name (I'm using "ChangeStat"for this example).
-- Script located anywhere (preferably ServerScriptService) -- Shorthand location local repst = game.ReplicatedStorage -- OnPlayerAdded, establish stats game.Players.PlayerAdded:Connect(function() Level = Instance.new("IntValue") Level.Parent = Player XP = Instance.new("IntValue") XP.Parent = Player XPCap = Instance.new("IntValue") XPCap.Parent = Player end) -- Listens for the RemoteFunction named "ChangeStat" repst.ChangeStat.OnServerInvoke = function (player, stat, value) -- Changes the Instance's value to the value specified player.stat.Value = value end
Now we have established a way for the client to call the server to change the stat located inside our player. All that's left is to call it through a local script.
-- LocalScript -- Shorthand location local repst = game.ReplicatedStorage -- Calls "ChangeStat" with specified parameters repst.ChangeStat:InvokeServer("Level", 2)
In short, use RemoteEvents/Functions in order to change values within the game from the client. Please take the time to understand the code I posted as well as the overall concept of this topic through the Roblox Developer Hub.
Hope this helps!