How do I get a player's leaderstats in a server script

I am trying to make a system like Sword Fighting Simulator where you swing a sword and the NPC takes damage from how much Power the player that hit the NPC has.

I know the problem is getting the player leaderstats, but I don’t know how to do it correctly.

This script is in the Sword (Normal Script)

This is in a part of a part of the sword (Normal Script)

function onTouch(hit)
if hit.Parent:FindFirstChild('HealthSpot') then --Checks for HealthSpot in NPC
local Sound = script.SoundEffect
script.Disabled = true
Sound:play() --Sound of the swords hitting the NPC
local Effect = script.Parent.Effect
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local animation = script.Animation
local animationtrack = humanoid.Animator:LoadAnimation(animation)
animationtrack:Play() --Makes NPC have a hit effect 
		
		game.Players:GetPlayerFromCharacter(hit.Parent) --Gets the player
		local Power = hit.Parent.leaderstats.Power
		humanoid = humanoid.Health - Power.Value --Damage taked to the NPC
	
		
Effect.Rate = 100 --The Partical Effet of the NPC getting hit
task.wait(0.5)
Effect.Rate = 0
end
end
script.Parent.Touched:Connect(onTouch)
2 Likes

On line 12 you aren’t setting the player object to anything. I think if you just make this simple change it should work:

		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Gets the player
		local Power = player.leaderstats.Power
		humanoid = humanoid.Health - Power.Value --Damage taked to the NPC

An Error pops up

Workspace.Cyber_Designer.StarterSword.Handle.MeshPart.Script:13: attempt to index nil with ‘leaderstats’

function onTouch(hit)
if hit.Parent:FindFirstChild('HealthSpot') then
local Sound = script.SoundEffect
script.Disabled = true
Sound:play()
local Effect = script.Parent.Effect
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local animation = script.Animation
local animationtrack = humanoid:LoadAnimation(animation)
animationtrack:Play()
		
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Gets the player
		local Power = player.leaderstats.Power
		humanoid.Health = humanoid.Health - Power.Value --Damage taked to the NPC
		
Effect.Rate = 100
task.wait(0.5)
Effect.Rate = 0
end
end
script.Parent.Touched:Connect(onTouch)

The script is in a part of a part of the sword
(StarterSword - Handle - MeshPart - Script)

Have you tested it yet with any player? NPC’s aren’t counted as players. Try with actual players if not done yet.

I have not tested with players but I am wanting the leaderstats from the player with the sword

You’re hitting players correct? Because if you are’nt then you can’t get the leaderstats.

I am wanting to do this

I want the players power to be the amount of damage to the NPC

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

since youre hitting the NPC this will try to get the NPC’s player not get the user’s player. this should be returning nil because the NPC isnt a player in players.

1 Like

how do I get the player in the same script

You would have to keep the same code but change the hit.Parent to script.Parent.Parent.Parent.Parent

(assuming the StarterSword is in the player’s character)

1 Like

That worked and did what I want THANK YOU!!!

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