Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I base a sword's damage off a player's leaderstat?

Asked by
4d0z 4
5 years ago

Exactly what the title says.

I have this sword and I want the damage to be based off a player's leaderstat. For example, if I have 700 power, it's multiplied by .07 and then that's the damage value.

Here's the script I have in the sword:

wait(0.01)
CanDamage = true



script.Parent.Handle.Touched:connect(function(hum) -- Changed "part" to "hum"
   if hum and hum.Parent:FindFirstChild("Humanoid") then -- "hum" is the Player who touched the part/Sword.
local player = game.Players:GetPlayerFromCharacter(hum.Parent)
local plr = player.leaderstats.Power.Value
           if CanDamage == true then
               CanDamage = false
               hum.Parent.Humanoid:TakeDamage(plr * 0.07) -- I forgot to add the "Parent.Humanoid" part after "hum"1
               wait(0.8)
               CanDamage = true
           end
       end

end)

No errors pop up aswell.

I'd appreciate the help!

0
Try and debug first. Set up print()'s around the script, especially after each if statement. See where it stops printing. Shawnyg 4330 — 5y
0
I think you are taking the hit players value, I understand that you want to take yours value I think. Torren_Mr 334 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

ok?

wait(0.01)
CanDamage = true
script.Parent.Handle.Touched:connect(function(hum) -- Changed "part" to "hum"
   if hum and hum.Parent:FindFirstChild("Humanoid") then -- "hum" is the Player who touched the part/Sword.
local player = game.Players:GetPlayerFromCharacter(hum.Parent)
local plr = player.leaderstats.Power.Value
 local damage = math.ceil(plr * 0.07)
           if CanDamage == true then
               CanDamage = false
               hum.Parent.Humanoid:TakeDamage(damage) -- I forgot to add 
               print(damage)
               wait(0.8)
               CanDamage = true
           end
       end
end)
0
remove a math.ceil at line 07 if u want dark_nineret -25 — 5y
0
00f dark_nineret -25 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You have to get the plyer who owns the sword, not the one you hit, I assume this is a server script, and there should be a local script firing an animation or something, So in that local script, fire a Remote Event instance. These remotes are used for getting the polayer on server scripts, this is how it should look like:

--LocalScript
local Remote = game.ReplicatedStorage.RemoteEvent
Remote:FireServer()

--Server Script

local CanDamage = true
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player) -- here we got the player 
    script.Parent.Handle.Touched:connect(function(hum)
        if hum and hum.Parent:FindFirstChild("Humanoid") then 
            local player = game.Players:GetPlayerFromCharacter(hum.Parent)
            local plr = player.leaderstats.Power.Value
            if CanDamage == true then
                       CanDamage = false
                       hum.Parent.Humanoid:TakeDamage(player.Data.Damage * 0.07)
                       wait(0.8)
                       CanDamage = true
               end
             end
      end)
end)

Remote Events just allow an access from client to server. And firing a remote when you use the sword will make the sword know the player who used it. I hope I helped.

0
I'm getting an error saying Handle is not a valid member of ServerScriptService / What am I doing wrong?? 4d0z 4 — 5y
0
You're firing the remote on a server script in serverscriptservice, why would u do that? Just put the server script inside of Handle. if you did FireServer good, it will work. EternalScythe 222 — 5y

Answer this question