Basically trying to find what a value a string value is and connecting it to a certain thing, this is what i currently have. Using a module script so the tool’s main script isnt flooded
local module = {}
module.Y = function()
if tool.T1.Value == "B" then
if hit.Name == "Head" then
character.Humanoid:TakeDamage(math.round((DmgV.Value * (gLevel.Value/10))*1.5))
elseif hit.Name == "Torso" then
character.Humanoid:TakeDamage(math.round((DmgV.Value * (gLevel.Value/10))))
else
character.Humanoid:TakeDamage(math.round((DmgV.Value * (gLevel.Value/10))*0.7))
end
elseif tool.T1.Value == "C" then
character.Humanoid:TakeDamage(math.round((DmgV.Value * (gLevel.Value/10))*math.random(0.00, 2.10)))
elseif tool.T1.Value == "D" then
if hit.Name == "Head" then
character.Humanoid:TakeDamage(math.round((DmgV.Value * (gLevel.Value/10))*1.5))
elseif hit.Name == "Torso" then
character.Humanoid:TakeDamage(math.round((DmgV.Value * (gLevel.Value/10)) + ((DmgV.Value * (gLevel.Value/10))/0.30)))
else
character.Humanoid:TakeDamage(math.round((DmgV.Value * (gLevel.Value/10))*0.7))
end
end
end
return module
local Stats = {
["B"] = {
["Head"] = 1.5,
["Torso"] = 1,
["Default"] = 0.7,
},
}
local DamageData = Stats[tool.T1.Value]
local DamageModifer = DamageData[Hit.Name] or DamageData.Default
local Damage = math.round(DmgV.Value * (gLevel.Value/10) * DamageModifer)
Humanoid:TakeDamage(Damage)
If you consider this better, would however have to create if statements for the math.random or add operation or have it accept number range value types and just do type checking
I saw each value of T1 would give a dynamic multiplier so I rewrote a better version using @Thedagz’s idea:
function module.Y()
local b = DmgV.Value * (gLevel.Value / 10);
local multipliers = {
B = function()
return (hit.Name == "Head" and 1.5) or hit.Name == "Torso" and 1 or 0.7;
end,
C = function()
return math.random(0.00, 2.10);
end,
D = function()
local a = b / 0.30;
local s = b + a;
return (hit.Name == "Head" and 1.5) or hit.Name == "Torso" and a / s or 0.7;
end,
}
local multFunc = multipliers[tool.T1.Value]
if multFunc then
character.Humanoid:TakeDamage(math.round(b * multFunc()));
end
end
Could probably combine both methods, check if value is a function and if so then call function with typeof() otherwise use data
local Stats = {
["B"] = {
["Head"] = 1.5,
["Torso"] = 1,
["Default"] = 0.7,
},
["C"] = function()
return math.random(0.00,2.10) -- this will only return a number 0 1 and 2
end
}
These are good, but im not just looking for damage multipliers, i just only included damage multipliers because they were the first thing that came to mind, mainly looking for a way to shorten the if statements
(some other T1 could make you move faster, make the hit oppenent move slower, on hit have a 20% chance to reobtain the bullet you shot, ect)
what i thought could work is using a module script to get another module script with the same name as the T1 Value
local module = {}
require(game.ReplicatedStorage.Talents.A)
module.Tfa = function()
local Tal1Find = tool.T1.Value
require(game.ReplicatedStorage.Talents:FindFirstChild(Tal1Find))
end
return module
and then the module would have the information like
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
M.Tfa()
end
end
delay(0, function()
bulletCache:ReturnPart(bullet)
end)
end
(Note, im not sure this even works i just came up with this idea like right now)
(Edit: Also sorry for not replying sooner, i almost forgot i made this)
(Edit2: Didnt work whatsoever)
well you can simply do raycast and then check for the char,player,the part it hit. if it is a success check tool value and the Player Talent. then make a dictionary for specific damage like i did and make a function and pass parameters like i did. this should help you reduce if else statements in your code as it directly checks. but pls do it in pcall as you are not using if else (mostly) it can throw errors if it didnt find the value in dictionary.
Not sure if its done correctly, just hoping it is
and then requiring it in the script
local M = require(game.ReplicatedStorage.Talents)
Now im wondering how i would put it into my main Hit function?
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
-- The damage functions would go here
end
delay(0, function()
bulletCache:ReturnPart(bullet)
end)
end
Ok so it’s real simple.
But this will add a function to your raycast function
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
local function DoDmg()
if hit then
character.Humanoid:TakeDamage(M["D"].hit
else
character.Humanoid:TakeDamage(M["C"]["none"]
end
delay(0, function()
bulletCache:ReturnPart(bullet)
end)
end
You might wonder why am I using “D” Talent here.
You need to pass that players talent aswell here for this to fire “M” if player has “M” Talent. Please update your script to send talent as a param aswell.
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
local function DoDmg()
if hit then
character.Humanoid:TakeDamage((M["D"].hit))
else
character.Humanoid:TakeDamage((M["C"]["none"]))
end
end
DoDmg()
end
delay(0, function()
bulletCache:ReturnPart(bullet)
end)
end
But when i use it it errors with TestService: Exception thrown in your RayHit event handler: Argument 1 missing or nil
I added print into the function
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
local function DoDmg()
if hit then
print(M["D"].hit)
character.Humanoid:TakeDamage((M["D"].hit))
else
character.Humanoid:TakeDamage((M["C"]["none"]))
end
end
DoDmg()
end
delay(0, function()
bulletCache:ReturnPart(bullet)
end)
end
and it prints nil for some reason?
(edit heres the module)
character.Humanoid:TakeDamage(Mff.C.Head) -- this works
while
character.Humanoid:TakeDamage(Mff.C[hit]) -- this doesn't work
so i need some sort of way to get the hit to C or something idk how to explain it, but it thinks there’s actually something named “hit” in the dictionary when there isnt
Ok tbh the if else doesnt seem that bad as if an exploiter tries to make a new rank/talent then it will break the whole script. But lets say i found a way. Also i seee wwhy its not worrking. You should movee thee table to the script in whichh u have ur function.
as the tables[] operator works in normal scripts. Or make it a Global.
So take the table and put it in your ray cast script.
Also i found a error with thhe dictionary
You need to change the none and replace it with other parts (both r6 and r15) because it wont find none that easily. Same for “c”.
So your dictionary should be:-
local dict =
{
["B"] = { ["all body parts..."] --= respective damage for each part. In your case for head and torso its different and for rest its same. For "C" just list all parts and give all the default damage for "C".for "D" do the same as above. Now i think your script which had else ifs was better. I strongly reccomend you to stick to your script. But if u still think that else statements are not good enough. You are welcome to try dictionaries. Also i reccomend you to pass player stats as a variable in your OnRayHit function. It can reduce your code and make it more fast.