Problem
I'm trying to make a level up System where if the players XP is higher than Excap it will subtract from Excap's value. All this works in studio but not in game. PlayerAdded can only work in Server Script. And FireClient isn't working. Unless I did something wrong. Can I get some help?
Server Script
game.Players.PlayerAdded:connect(function(Player) local Data = Instance.new("IntValue") Data.Name = "Data" Data.Parent = Player local XP = Instance.new("IntValue") XP.Name = "XP" XP.Value = 0 XP.Parent = Data local Level = Instance.new("IntValue") Level.Name = "Level" Level.Value = 1 Level.Parent = Data local Tries = Instance.new("IntValue") Tries.Value = 10 Tries.Name = "Tries" Tries.Parent = Data local ExCap = Instance.new("IntValue") ExCap.Value = 500 ExCap.Name = "ExCap" ExCap.Parent = Data XP.Changed:Connect(function() XPChange(Player,XP,Level,ExCap) end) end) function XPChange(Player, XP, Level, ExCap) game.ReplicatedStorage.RemoteEvent:FireClient(Player, XP,Level,ExCap) end
Local Script
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(XP,Level,ExCap) if XP.Value >= ExCap.Value then if Level.Value < 100 then Level.Value = Level.Value + 1; XP.Value = XP.Value-ExCap.Value; ExCap.Value = ExCap.Value * 2; end end if Level.Value == 100 and XP.Value ~= 0 then XP.Value = 0; end end)
You should not use remote here cuz game.Players
is server-sided so you can change things in the players without localscript.
i'll will try my best to guide you through this.(sry for my bad english ;P xP)
I dont know what you want "Tries" for and i dont see it beeing used in the script so i'll skip it.
At the beginning you should create a datastore for each instance.new
xps = game:GetService("DataStoreService"):GetDataStore("xps") -- This will store the xp levels = game:GetService("DataStoreService"):GetDataStore("levels") -- This will store the level Excap = game:GetService("DataStoreService"):GetDataStore("Excap") -- The amount of xp needed
I wont explain this part much cuz you should now this: I'll explain GetAsync later
game.Players.PlayerAdded:Connect(function(player) stats = Instance.new("Folder", player) -- Lets make a leaderstats. It will be easier to see if it works then. stats.Name = "leaderstats" xp = Instance.new("IntValue", leaderstats) xp.Name = "XP" xp.Value = xps:GetAsync(player.UserId) or 0 level = Instance.new("IntValue", leaderstats) level.Name = "Level" level.Value = levels:GetAsync(player.UserId) or 1 excap = Instance.new("IntValue", leaderstats) excap.Name = "Excap" excap.Value = excaps:GetAsync(player.UserId) or 500 end)
Now lets save the data if the values change
This will be inside the game.Players.PlayerAdded:Connect(function(player)
Set/GetAsync uses to set or load data. in this case we set the data to the players userid and the data we will set is xp,level and excap. The GetAsync abow will get the data from the player who joined. If the player doesnt have a data it will be set to the thing after or
xp.Value.Changed:Connect(function() xps:SetAsync(player.UserId, xp.Value) end) level.Value.Changed:Connect(function() levels:SetAsync(player.UserId, level.Value) end) excap.Value.Changed:Connect(function() excaps:SetAsync(player.UserId, excap.Value) end)
and now lets add your calculations inside the game.Players.PlayerAdded:Connect(function(player)
aswell.
We dont need these ;
if xp.Value >= excap.Value then if level.Value < 100 then level.Value = level.Value + 1 xp.Value = xp.Value-excap.Value excap.Value = excap.Value * 2 end end if level.Value == 100 and xp.Value ~= 0 then xp.Value = 0 end
So in the end it will be something like this
xps = game:GetService("DataStoreService"):GetDataStore("xps") levels = game:GetService("DataStoreService"):GetDataStore("levels") excaps = game:GetService("DataStoreService"):GetDataStore("Excaps") game.Players.PlayerAdded:Connect(function(player) stats = Instance.new("Folder", player) stats.Name = "leaderstats" xp = Instance.new("IntValue", leaderstats) xp.Name = "XP" xp.Value = xps:GetAsync(player.UserId) or 0 level = Instance.new("IntValue", leaderstats) level.Name = "Level" level.Value = levels:GetAsync(player.UserId) or 1 excap = Instance.new("IntValue", leaderstats) excap.Name = "Excap" excap.Value = excaps:GetAsync(player.UserId) or 500 xp.Value.Changed:Connect(function() xps:SetAsync(player.UserId, xp.Value) end) level.ValueChanged:Connect(function() levels:SetAsync(player.UserId, level.Value) end) excap.ValueChanged:Connect(function() excaps:SetAsync(player.UserId, excap.Value) end) if xp.Value >= excap.Value then if level.Value < 100 then level.Value = level.Value + 1 xp.Value = xp.Value-excap.Value excap.Value = excap.Value * 2 end end if level.Value == 100 and xp.Value ~= 0 then xp.Value = 0 end end)