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