Level/XP system Doesn't work when a local script gives the player XP, only regular scripts work

So I have a fully functional Level/XP system, I tested it out by putting a brick down and putting a script into the brick that says:

local debounce = false

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		
		if debounce == false then
			debounce = true
			
			local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			player.LevelSystem.Exp.Value += 10
			wait(1)
			debounce = false
		end
	end
end)

When I touch the brick, it works fine and I get the xp, and once I reach the xp goal to level up, It levels me up and resets the xp, It works fine. BUT, When I tried to give myself xp from killing players, the xp goes up just fine, but once it reaches the xp goal to level up, It doesn’t level up and keeps adding on to the previous xp, there are no errors in the output so I’m confused, here’s the local script for gaining kill xp.

-- // Services

-- External services
local tweenService = game:GetService("TweenService")

-- Default services
local replicatedStorage = game:GetService("ReplicatedStorage")
local playersService = game:GetService("Players")

-- // Variables

-- Comms variables
local killEvent = replicatedStorage:WaitForChild("KillNotification")

-- Player variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

-- UI variables
local killGUI = script.Parent
local brightness = killGUI:WaitForChild("Brightness")
local notification = killGUI:WaitForChild("Notification")
local Cash = killGUI:WaitForChild("Cash")

local killSound = script:WaitForChild("KillSound")

-- // Functions

local function tweenTransparency(guiObject, duration, transparency, Override)
	spawn(function()

		local tween = nil

		if guiObject:IsA("TextLabel") then
			local tween = tweenService:Create(guiObject, TweenInfo.new(duration), {TextTransparency = transparency}, {Override = Override})

			tween:Play()

			spawn(function()
				tween.Completed:Wait()
				tween:Destroy()
			end)
		elseif guiObject:IsA("ImageLabel") then
			local tween = tweenService:Create(guiObject, TweenInfo.new(duration), {ImageTransparency = transparency}, {Override = Override})

			tween:Play()

			spawn(function()
				tween.Completed:Wait()
				tween:Destroy()
			end)
		end

		if tween ~= nil then
			return tween
		end

	end)
end


local activeTextLabel = script.Value
local function notify(victim)
	player.LevelSystem.Exp.Value += 10
	if activeTextLabel == true then
		repeat
			wait(.05)
		until not activeTextLabel == true
	end
	activeTextLabel = true
	notification.TextTransparency = 1
	Cash.TextTransparency = 1
	notification.Text = "<font color='#ff3a3a'>Eliminated: </font>" .. victim.Name

	killSound:Play()
	tweenTransparency(notification, 0.5, 0, true)
	tweenTransparency(Cash, 0.5, 0, true)
	notification:TweenSize(UDim2.new(0.302, 0, 0.052, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.2, true)
	Cash:TweenSize(UDim2.new(0.302, 0, 0.071, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.2, true)
	wait(0.1)
	notification:TweenSize(UDim2.new(0.216, 0, 0.037, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.2, true)
	Cash:TweenSize(UDim2.new(0.216, 0, 0.051, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.2, true)
	wait(1.5)
	tweenTransparency(Cash, 0.3, 1, true)
	tweenTransparency(notification, 0.3, 1, true)
	activeTextLabel = false
end


-- // Connections
killEvent.OnClientEvent:Connect(notify)

The XP is given on line 65 where it says
player.LevelSystem.Exp.Value += 10

Here is the script inside of ServerScriptService that causes the player to level up and reset their xp once their XP reaches the level up goal.

game.Players.PlayerAdded:Connect(function(Player)
	
	
	
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "LevelSystem"
	
	local Level = Instance.new("NumberValue", leaderstats)
	Level.Name = "Level"
	Level.Value = 1	
	
	local Exp = Instance.new("NumberValue", leaderstats)
	Exp.Name = "Exp"
	Exp.Value = 0
	
	local RequiredExp = Instance.new("NumberValue", leaderstats)
	RequiredExp.Name = "RequiredExp"
	RequiredExp.Value = Level.Value * 100



	
	

	Exp.Changed:Connect(function(Changed)
		if Exp.value >= RequiredExp.Value then
			Exp.Value = 0
			
			Level.Value += 1
			RequiredExp.Value = Level.Value * 10
		end
	end)
end)

How can I solve this issue?

You might need to familiarize yourself with LocalScripts more, and the how the client/server model works.

Changes to the datamodel, such as property changes (setting the value of an object) are not replicated to the server. The server will not accept that information, but the client will continue to see it as it was last set. To fix this, you will want a server script to do this kind of a change.

1 Like

Please read up on the Client-Server model, changes in localscripts do not replicate to the server.

2 Likes

Ah I see, I probably shouldn’t have given the player XP through a client sided notification script.