Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

2 comments

0

Try and debug first. Set up print()'s around the script, especially after each if statement. See where it stops printing.

0

I think you are taking the hit players value, I understand that you want to take yours value I think.

2 answers

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)

2 comments

0

remove a math.ceil at line 07 if u want

0

00f

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.

2 comments

0

I'm getting an error saying Handle is not a valid member of ServerScriptService / What am I doing wrong??

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.