I’m a little developer that currently making a game. I want to implement an level XP system to my game. I have some basic knowledge of how to make it, but since the first one I made is pretty bad, and I got some suggestions from others, I decided to make a new one. I used Humanoid.Died to give the XP to player since I’m using leaderstats, but for some reason it does not fired. Does anyone knows why it didn’t fired?
Inside of ServerScriptStorage
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local XP = Instance.new("IntValue",leaderstats)
XP.Name = "XP"
end)
Inside of an enemy
local Enemy = script.Parent.Humanoid
if Enemy.Health <= 0 then
Enemy.Health = 0
end
Enemy.Died:Connect(function(player)
print("It is dead!")
local PlayerXP = player.leaderstats.XP
PlayerXP.Value = PlayerXP.Value + 1
end)
If so, this won’t update the PlayerXP value to the server side because of FilteringEnabled. This is just a secure measure to make sure that this value doesn’t replicate to the server in the event of an exploiter.
Read the thing I linked, I made a brief description on this scenario, but there is a lot more too it!
I’m not sure if this is the appropriate way, but why not fetch all the current enemies in a for loop in server (optional), detect .died from server, have a child added event (to detect new enemy spawning)
One thing I just noticed if that you assign the player as a parameter? how do you know which player killed the enemy? there should be some sort of tag on who killed the enemy. You need a kill check
also yea you need to change the values of xp via server you should not allow the client to dictate how much xp one has
Humanoid.Died takes no parameters, it doesn’t return anything. What you should do is have an ObjectValue pointing to the player when the enemy dies, then give that player money
ah ok, but the thing is how to I do that? I just learned Luau 6 months ago so I’ll consider myself as a beginner, or a noob, or whatever you want to call me as
I’m trying to give the player that killed the enemy XP, I know there’s a thing to can make in a script that is called tag, but I’m not sure how to do it.
Ah I see the tag, I also see how it works when hit an enemy. So right now If I can just get the tag then send those XP to the player then everything is fine right?
Yes, the creator tag that is added to the person who’s been hit and has the value of the person’s name who hit them. You just use a single server script (should be placed in ServerScriptService) to check who’s eliminated who and give the enemy the XP.
This should accomplish what you’re looking for:
game.Players.PlayerAdded:Connect(function(player) --Creating leaderstats: as before
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local XP = Instance.new("IntValue",leaderstats)
XP.Name = "XP"
while true do --Character load
if player.Character ~= nil then break
end
wait(5)
end
leaderstats.Parent = player
local function getEnemy(humanoid) --Finding the enemy
local Swordtag = humanoid:FindFirstChild("creator") ---Getting the tag the default sword adds
if Swordtag ~= nil then
local enemy = Swordtag.Value
if enemy.Parent ~= nil then
return enemy --Getting enemy to add XP in died event
end
end
return nil
end
local humanoid = player.Character:WaitForChild("Humanoid")
humanoid.Died:connect(function() --When player dies
local killer = getEnemy(humanoid)
if killer ~= nil then
local leaderstats = killer:FindFirstChild("leaderstats")
if leaderstats ~= nil then
local enemyxp = leaderstats:FindFirstChild("XP")
if killer ~= player then
enemyxp.Value = enemyxp.Value + 1 --Giving enemy XP
else
--
end
end
end
end)