Need Help with Random Sword Damages

So I have an issue where I’m trying to have different random damages for different weapons. I have the humanoid take damage when I hit it, and I want the humanoid to detect what weapon hits it, and then reference a module script with the different damage numbers for each weapon in a table. Here’s the Server script that fires the hit event:

local repStorage = game:GetService("ReplicatedStorage")
local onHitEvent = repStorage.onHitEvent

local knife = game.StarterPack.Knife
local weaponDmg = require(repStorage.WeaponDamages)
local Damage = weaponDmg.Knife

onHitEvent.OnServerEvent:Connect(function(plr, hitHum)
	if not hitHum then return end
	
	hitHum:TakeDamage(Damage)
end)

And this is the ModuleScript with all of the weapon damage info:

return {
	["Knife"] = 5, 8,
	["Water Sword"] = 7, 15,
	["Earth Sword"] = 12, 18,
	["Fire Sword"] = 14, 19,
	["Elemental Sword"] = 17, 23,
	["Blade of Night"] = 19, 28
}

When I try to use the TakeDamage method using math.random, I can’t seem to get the damage amount to be right or correlate with the damage in the table. As mentioned above, I also want to have the humanoid detect which weapon hit it so that I can have different damages for different weapons.

1 Like

That not how it works really;
You essentially did:

return {
	["Knife"] = 5,
	[1]=8,
	["Water Sword"] = 7, 
	[2]=15,
	["Earth Sword"] = 12, 
	[3]=18,
	["Fire Sword"] = 14, 
	[4]=19,
	["Elemental Sword"] = 17, 
	[5]=23,
	["Blade of Night"] = 19, 
	[6]=28
}

What you can do:

export type ToolDamage = {Min:number;Max:number}
return {
	["Knife"] = {Min=5,Max=8};
	["Water Sword"] = {Min=7,Max=15}; 
	["Earth Sword"] = {Min=12,Max=18};
	["Fire Sword"] = {Min=14,Max=19};
	["Elemental Sword"] = {Min=17,Max=23};
	["Blade of Night"] = {Min=19,Max=28}; 
}::{[string]:ToolDamage}
local repStorage = game:GetService("ReplicatedStorage")
local onHitEvent = repStorage.onHitEvent

local knife = game.StarterPack.Knife
local weaponDmg = require(repStorage.WeaponDamages)
local Knife_Tool_Damage = weaponDmg.Knife
local Min,Max = Knife_Tool_Damage.Min,Knife_Tool_Damage.Max

onHitEvent.OnServerEvent:Connect(function(plr, hitHum)
	if not hitHum then return end
	
	hitHum:TakeDamage(math.random(Min,Max))
end)

Also please do sanity checks.

2 Likes

This could just be me, but I can’t really change the string from “Knife” to any of the other weapons in the table. Is there a way to still use hitHum:TakeDamage(math.random(Min, Max)), but make it apply to any of the other weapons?

1 Like

Explain.
Are you trying to grab value of other weapons?

1 Like

I want to be able to use hitHum:TakeDamage(math.random(Min, Max)) once, but the values for Min and Max are different depending which one of those weapons you are currently using, yes.

1 Like

So I guess yes, I do want to grab the values of the other weapons.

1 Like

Whatever you are doing seems like an actual game project, and instead I suggest you restrain yourself for at least 2 months and lock in on learning about sanity checks and about the language Luau itself as well as the Roblox API.
Things you are doing are very prone to exploits and not very scalable overall.

2 months? even i cant wait that long

my advice: chase your dreams buddy.

my psychology is “problem? solve it later im too lazy”

1 Like

You aren’t really being helpful by pointing this out. Yes, it is a project, that’s not your problem. I’ll learn about it, but just be conducive to the conversation and stop pointing out my shortcomings, please.

BTW if you have any solutions I would love to hear them.

Yes, you can. I’m not able to help you with the client side code as it wasn’t given but I can help you with the server script.

Lets take a look at @Yarik_superpro 's script:

local repStorage = game:GetService("ReplicatedStorage")
local onHitEvent = repStorage.onHitEvent

local knife = game.StarterPack.Knife
local weaponDmg = require(repStorage.WeaponDamages)
local Knife_Tool_Damage = weaponDmg.Knife
local Min,Max = Knife_Tool_Damage.Min,Knife_Tool_Damage.Max

onHitEvent.OnServerEvent:Connect(function(plr, hitHum)
	if not hitHum then return end
	
	hitHum:TakeDamage(math.random(Min,Max))
end)

This script works exclusively for the knife because the damage is set outside any functions meaning it cannot be changed again for any other weapons.

Here’s a modified version of the script:

local repStorage = game:GetService("ReplicatedStorage")
local onHitEvent = repStorage.onHitEvent
local weaponDmg = require(repStorage.WeaponDamages)

--local knife = game.StarterPack.Knife
--local Knife_Tool_Damage = weaponDmg.Knife
--local Min,Max = Knife_Tool_Damage.Min,Knife_Tool_Damage.Max

--These  above lines are no longer necessary

onHitEvent.OnServerEvent:Connect(function(plr, hitHum, weaponName:string) --added a new parameter so the script can identify the weapon
	if not hitHum then return end
	local weaponData = weaponDmg[weaponName] --getting the weapon's data from the table
--weaponDmg[weaponName] is the similar to weaponDmg.weaponName but it works for strings and string variables
	local Min, Max = weaponData.Min , weaponData.Max --retrieve the weapon's damage range

	hitHum:TakeDamage(math.random(Min,Max))
end)
3 Likes

Pointing out shortcomings is being helpful. If you can’t confront them now, they’ll sabotage you later.

It’s not what you said, it’s how you said it

It’s not how i said it, It’s how you interpret it.

well atp im willing to do whatever
steal a pretzel bag wont develop itself

1 Like

I just wanted you to answer the question dude. I’m not afraid to admit to my own shortcomings. That doesn’t change the fact that I can still have a project to work on. If you can’t just answer the question then please go somewhere else.

1 Like

yeah this works @noob112034 , you need to get the key for the currently equipped tool and then from there you get the correct values

Is it really so hard to copy+paste this, vro? :wilted_flower: :broken_heart: :v:

So I’m running into an issue where local Min, Max = weaponData.Min, weaponData.Max is returning nil for Min.

Here’s what the client-side looks like:

local repStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(repStorage.RaycastHitboxV4)


Tool = script.Parent
Handle = Tool:WaitForChild("Handle")
local animation1 = Handle:WaitForChild("HoldAnim")
local attkAnim1 = Handle:WaitForChild("Attack1")
local attkAnim2 = Handle:WaitForChild("Attack2")

local hitboxPart = Tool:WaitForChild("Hitbox")
local newHitbox = RaycastHitbox.new(hitboxPart)
local onHitEvent = repStorage:WaitForChild("onHitEvent")

--print(newHitbox)

local debounce = 0.1
local isHolding = false
local activated = false

local atk1Played = false
local atk2Played = false

Tool.Equipped:Connect(function()
	local humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")
	local holdAnim = humanoid:LoadAnimation(animation1)

	if animation1 and humanoid then

		holdAnim:Play()

		isHolding = true
	end

	Tool.Unequipped:Connect(function()
		isHolding = false

		holdAnim:Stop()
	end)
end)

local function attack1Played()

	local humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")
	local Attack1 = humanoid:LoadAnimation(attkAnim1)

	if humanoid and isHolding == true and activated == false then

		activated = true
		atk2Played = false

		newHitbox:HitStart()

		Attack1:Play()
		task.wait(1.25)
		Attack1:Stop()

		newHitbox:HitStop()

		task.wait(debounce)


		activated = false
		atk1Played = true
	end
end

local function attack2Played()

	local humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")
	local Attack2 = humanoid:LoadAnimation(attkAnim2)

	if humanoid and atk1Played == true then

		atk1Played = false
		activated = true

		newHitbox:HitStart()

		Attack2:Play()
		task.wait(1.2)
		Attack2:Stop()

		newHitbox:HitStop()

		task.wait(debounce)



		activated = false
		atk2Played = true
	end
end

newHitbox.OnHit:Connect(function(hitPart, hitHum)
	local myHum = Tool.Parent:FindFirstChildOfClass("Humanoid")
	if not myHum then return end
	if myHum == hitHum then return end

	onHitEvent:FireServer(hitHum)
end)

Tool.Activated:Connect(attack1Played)
Tool.Activated:Connect(attack2Played)

Hope this helps.

Global variable :broken_heart: :v:

That not what debounce is vro :wilted_flower: