Ad
Log in to vote
0

how to make a punch do damage(also add health) according to leader stats?

Asked by 12 days ago
Edited 12 days ago

here is the first script(its in serverscriptservice)

PowerData = game:GetService("DataStoreService"):GetDataStore("PowerData")
RebirthsData = game:GetService("DataStoreService"):GetDataStore("RebirthsData")

game.Players.PlayerAdded:Connect(function(plr)
    local LB = Instance.new("IntValue")
    LB.Name = "leaderstats"
    LB.Parent = plr                                                     --data store script/leader stats script

    local p = Instance.new("IntValue")
    p.Name = "Power"
    p.Parent = LB
    p.Value = PowerData:GetAsync(plr.userId) or 0

    local r = Instance.new("IntValue")
    r.Name = "Rebirths"
    r.Parent = LB
    r.Value = RebirthsData:GetAsync(plr.userId) or 0
end)

game.Players.PlayerRemoving:Connect(function(plr)
    PowerData:SetAsync(plr.userId,plr.leaderstats.Power.Value)
    RebirthsData:SetAsync(plr.userId,plr.leaderstats.Rebirths.Value)
end)

here is the second script(its in starter player scripts)

local Player = game.Players.LocalPlayerscript.Parent.LeftHand.Touched:connect(function(hit)
   if hit.Parent.Humanoid and dmg == true then
    hit.Parent.Humanoid:TakeDamage(20)
local Mouse = Player:GetMouse()
local dmg = true

Mouse.KeyDown:connect(function(key)                                                 --punch script
 if key == "q" then 
  local an = script.Parent.Humanoid:LoadAnimation(script.Animation)
  an:Play()

    dmg = false
    wait(1)
    dmg = true
wait(2)
   end
  end)
 end
end)

1 answer

Log in to vote
0
Answered by
oilsauce 100
12 days ago

It's a quite easy fix, you just have to multiply the damage with your stats.

-- Localscript

local Player = game.Players.LocalPlayer
local LB = Player:WaitForChild("leaderstats")
local p = LB:WaitForChild("Power")
local damage = 20 * p.Value

--------------

Humanoid:TakeDamage(damage)

This will multiply the damage that the Humanoid will receive by your 'Power' value.

Also, you should use a Changed() Event for the 'Power' value, so the damage will increase every time your 'Power' value increases.

-- Localscript

p.Changed:Connect(function()
    local damage = 20 * p.Value
end)

One more thing, your script will make you able to run the KeyDown event when your Character's Left Hand is touched. You need to put the KeyDown event outside of the Touched event.

local Player = game.Players.LocalPlayer
local LB = Player:WaitForChild("leaderstats")
local p = LB:WaitForChild("Power")
local damage = 20 * p.Value

script.Parent.LeftHand.Touched:connect(function(hit)
   if hit.Parent.Humanoid and dmg == true then
    hit.Parent.Humanoid:TakeDamage(damage)
local Mouse = Player:GetMouse()
local dmg = true
 end
end)

Mouse.KeyDown:connect(function(key)                                                 --punch script
 if key == "q" then 
  local an = script.Parent.Humanoid:LoadAnimation(script.Animation)
  an:Play()

  dmg = false
  wait(1)
  dmg = true
  wait(2)
 end
end)

If you have any questions, please feel free to ask them. I hope I helped.

Ad

Answer this question