Level system don't work

Hi! I’m a small developer and I’m been working on a small game. I added a level system to my game 2 months ago. However, for some reason it didn’t work. I started to learn Lua when it’s January and I would still consider myself as a beginner. I really need help as this will affect my gameplay. Here’s the following script:

(inside of an enemy)

script.Parent.zombie.Died:Connect(function()
--to check if it is dead or not
	print('It is dead!')
	game.ReplicatedStorage.XPtrigger:FireClient()
	if game.ReplicatedStorage.XPtrigger:FireClient() then
--check if it was fired
		print('It fired!')
	end
end)

(Inside of StarterPlayersScripts)

game.ReplicatedStorage.XPtrigger.OnClientEvent:Connect(function()
-- check if it received the firing event from the remote event
	print('It receive!')
	script.Parent.Parent.LevelsInfo.playerXP.Value += 15
end)

The problem with this is that for some reason it didn’t work. It can detect the enemy is dead but it didn’t fired the event or the local script didn’t received the firing. I used explorer and found out that even if the enemy is killed the IntValue for the XP still didn’t go up in the client nor the server. I really want to know what is wrong with these scripts because Roblox didn’t find any scripting errors from these scripts and none of my family nor my friends know how to script. Pls reply to me asap as this has been a problem for 2 months

(Also btw I just become a member so I can finally post yay!)

2 Likes

Your RemoteEvent that is Firing the Client is missing a player specific argument.
When FireClient is used, it needs to know which client it is referring to.

This is what is written on the API

local function onPlayerAdded(player)
	welcomePlayerEvent:FireClient(player)
    -- the player here is the local player
end
 
Players.PlayerAdded:Connect(onPlayerAdded)
2 Likes

It depends on what you’re doing, if you want xp to be given to everyone, use :FireAllClients(). If you want the xp to be given to a specific player, in your weapon, you want to “tag” the player that hit the zombie onto the zombie, so thus when it dies, you can grab that tag and fire the xp directly to the player.

If it is the second case (as stated above), then please send the script that deals damage to the zombie and I can add the “tagging” system for you :slight_smile:

Managing the players level on the client end is a bad idea. You can not save this value or use it in any server scripts.

What you should do is set the players XP value on the server end. But you also have to find the player who killed the zombie. This is entirely dependant on the weapon scripts you are using. You should tag the zombie with the name of the player whenever they damage it, and then use that.

1 Like

As Milkzul stated above, changing something on the client end won’t do anything, so firing it to the client would be useless. Unless you want to display a gui when the player gets xp, you should completely remove the client part.

I did have a GUI that is connected to the XP IntValue. And for now I just use a basic classic sword in the roblox studio for testing. I don’t know that you shouldn’t do things on the client sorry I’m a noob.

Yes I am trying to give the XP to the player(s) that damage the enemy, also I just use a classic sword from the toolbox for now to test the XP system

so basically I should let the game know who damage the enemy, then let the game to fire client to give that player the XP

Well you should just change the xp in the server script and use the fire client to do the ui

ok but how should I do, like an example, because I don’t know really know how to script

You need to put the player instance in the brackets and then the code in the client with event.OnServerEvent.

Do u mind sending the damage script? I can modify that.

sure, here’s the script inside of the classic sword in the toolbox:

--Rescripted by Luckymaxer
--EUROCOW WAS HERE BECAUSE I MADE THE PARTICLES AND THEREFORE THIS ENTIRE SWORD PRETTY AND LOOK PRETTY WORDS AND I'D LIKE TO DEDICATE THIS TO MY FRIENDS AND HI LUCKYMAXER PLS FIX SFOTH SWORDS TY LOVE Y'ALl
--Updated for R15 avatars by StarWars
--Re-updated by TakeoHonorable

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
	BaseDamage = 5,
	SlashDamage = 10,
	LungeDamage = 30
}

--For R15 avatars
Animations = {
	R15Slash = 522635514,
	R15Lunge = 522638767
}

Damage = DamageValues.BaseDamage

Grips = {
	Up = CFrame.new(0, 0, -1.70000005, 0, 0, 1, 1, 0, 0, 0, 1, 0),
	Out = CFrame.new(0, 0, -1.70000005, 0, 1, 0, 1, -0, 0, 0, 0, -1)
}

Sounds = {
	Slash = Handle:WaitForChild("SwordSlash"),
	Lunge = Handle:WaitForChild("SwordLunge"),
	Unsheath = Handle:WaitForChild("Unsheath")
}

ToolEquipped = false

--For Omega Rainbow Katana thumbnail to display a lot of particles.
for i, v in pairs(Handle:GetChildren()) do
	if v:IsA("ParticleEmitter") then
		v.Rate = 20
	end
end

Tool.Grip = Grips.Up
Tool.Enabled = true

function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)	
end


function Attack()
	Damage = DamageValues.SlashDamage
	Sounds.Slash:Play()

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Slash"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Slash")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
end

function Lunge()
	Damage = DamageValues.LungeDamage

	Sounds.Lunge:Play()
	
	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Lunge"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Lunge")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
	--[[
	if CheckIfAlive() then
		local Force = Instance.new("BodyVelocity")
		Force.velocity = Vector3.new(0, 10, 0) 
		Force.maxForce = Vector3.new(0, 4000, 0)
		Debris:AddItem(Force, 0.4)
		Force.Parent = Torso
	end
	]]
	
	wait(0.2)
	Tool.Grip = Grips.Out
	wait(0.6)
	Tool.Grip = Grips.Up

	Damage = DamageValues.SlashDamage
end

Tool.Enabled = true
LastAttack = 0

function Activated()
	if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
		return
	end
	Tool.Enabled = false
	local Tick = RunService.Stepped:wait()
	if (Tick - LastAttack < 0.2) then
		Lunge()
	else
		Attack()
	end
	LastAttack = Tick
	--wait(0.5)
	Damage = DamageValues.BaseDamage
	local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl .. Animations.R15Slash,
		Parent = Tool
	})
	
	local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
		Name = "R15Lunge",
		AnimationId = BaseUrl .. Animations.R15Lunge,
		Parent = Tool
	})
	Tool.Enabled = true
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

function Unequipped()
	Tool.Grip = Grips.Up
	ToolEquipped = false
end

Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)

There appears to already have a tag system, so I’Ll write the server script for the zombie thing when I get on pc.

–Zombie Script

script.Parent.zombie.Humanoid.Died:Connect(function()
	print('It is dead!')
	script.Parent.Parent.LevelsInfo.playerXP.Value += 15 --Put the directory to the playerXP 
	game.ReplicatedStorage.XPtrigger:FireClient(game.Players[script.Parent.Zombie.creator.Value])
end)

–Local

game.ReplicatedStorage.XPtrigger.OnClientEvent:Connect(function()
	print('Received!')
    script.Parent.XP.Visible = true -- Directory to the ui
    wait(1)
    script.Parent.XP.Visible = false
end)

thx man! I’ll now try it out to see if it works

wait I just found out that should not work because the IntValue is inside of a player, not in workspace, so this might won’t work. That means I must use a local script for it, any suggestions how to change it or use another way to code?

well i put that you have to put the directory to the levels lol. I’m not sure where they are located.

Sorry if I didn’t tell you, but the enemy + the enemy script is in the workspace, the XP IntValue is inside of the LevelsInfo folder and the folder is inside of the player itself by using Instance.new . Also my GUI (It is just a GUI that shows the level + the XP you currently have) is located inside of the StarterGUI and I already coded it to linked to the XP IntValue.