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)
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
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)
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.