Comparing two players leaderstats not working

  1. What do you want to achieve? Keep it simple and clear!
    I want it so that when a player hits another player that is smaller, the smaller player dies and the bigger one lives.
  2. What is the issue? Include screenshots / videos if possible!
    Everything including the punch, and punch animation works, but when I compare the two players leaderstats it just doesn’t work.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have been picking at this issue for a few days and tried looking for people with similar problems but cant find the solution.

Here is my code.

game.ReplicatedStorage.Remotes.Punch.OnServerEvent:Connect(function(player,damage)
	
	for i,target in pairs(game.Workspace:GetDescendants() ) do
		if target:IsA("Humanoid") and target.Parent.Name ~= player.Name then
			if (target.Parent.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < 8 then
			
				local character = player.Character or player.CharacterAdded:Wait()
				local Humanoid = player.Character.Humanoid

				
				
				local Dude = target.Parent.Parent
				local Humanoid = Dude:FindFirstChild("Humanoid")
				local leaderstats = player:FindFirstChild("leaderstats").Size.Value
				local EnemyLeaderstats = Dude:FindFirstChild("leaderstats").Size.Value
				
				if Humanoid and EnemyLeaderstats < leaderstats then
				Dude.Health = 0
					
				elseif Humanoid and EnemyLeaderstats > leaderstats then
					
					player.Health = 0
					
					
				end
				
				wait(1)
				
			end
		end
	end
	
end)

Might be a little messed up because I just copy and pasted it here and it doesn’t really fit the page.

2 Likes

are you getting any errors? first potential problem I see is that you set player.Health instead of player’s humanoid health to 0

Im not getting any kind of errors, but I will fix the player.health and see what happens. Edit: I am getting one error, it says " Script ‘ServerScriptService.Script’, Line 15 " So I think it might be a problem with my variables, but I still don’t really know how to fix them, I am fairly new at scripting.

it might be this line

local Dude = target.Parent.Parent

you checked if target:IsA("Humanoid") , that would mean target.Parent.Parent is likely Workspace so there wouldn’t be a leaderstat

to get the player object from a player character, you need to use player service

local plrService = game:GetService("Players")

local Dude = plrService:GetPlayerFromCharacter(target.Parent)

or use FindFirstChild

game.Players:FindFirstChild(target.Parent.Name)

Ok, one more step away, I used your plrService (thanks btw) and edited the script in a few ways, first off, I had two variables with the same name (Fixed) but the final step would now just be dealing the damage, since Dude is now a variable for the Player and not the humanoid, I cant deal damage to the “enemy” using that variable. How would a get the enemy’s humanoid? (I hope all that made sense)

if I’m not mistaken target is the enemy’s humanoid

so you can just do

target.Health = 0
1 Like

Oh lol forgot about that sorry, let me test it now.

It works now! Thank you for your time! I made your comment the answer, have a good day!

2 Likes

I have a questions.
Why do you set player health to 0, when there is a damage variable in the event function?
Although I don’t know what is the goal you’re trying to reach, I think this would make more sense.

local distance = 8

game.ReplicatedStorage.Remotes.Punch.OnServerEvent:Connect(function(player,damage)

	for i,target in pairs(game.Workspace:GetDescendants() ) do
		if target:IsA("Humanoid") and target.Parent.Name ~= player.Name then
			if (target.Parent.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < disatance then

				local humanoid = player.Character:WaitForChild("Humanoid")
				local leaderstats = player:FindFirstChild("leaderstats").Size.Value

				local enemy = game.Players:GetPlayerFromCharacter(target.Parent)
				local EnemyLeaderstats = enemy:FindFirstChild("leaderstats").Size.Value

				if EnemyLeaderstats < leaderstats then
					target:TakeDamage(damage)
				elseif EnemyLeaderstats > leaderstats then
					humanoid:TakeDamage(damage/2)
				end
				wait()
			else return
			end
		end
	end

end)

That does look a lot cleaner then my very messy scripting, but I am adding on to that script right now and plan on adding another “Mechanic” to my game where there is more specific details too how much damage is taken/given, for example I was thinking of a Gamepass or item where you could survive the attack with low health and thought that for me, using the health = 0 was just more simple for my little monkey brain, I hope that makes sense in any way.

1 Like

oh ok, yeah that does make sense.:+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.