Using fastcast and just wondering, if theres a way i can shorten this?
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
if game.ReplicatedStorage.Tals:FindFirstChild(gt.Value):FindFirstChild(tool.strings.T1.Value) then -- Talent 1
local TalFolder = game:GetService("ReplicatedStorage").Tals
local TalentMod = require(TalFolder:FindFirstChild(gt.Value):WaitForChild(tool.strings.T1.Value))
local strings = tool.strings
TalentMod.Yes(hit, strings, bullet, "T1")
else
local BaseGun = require(game:GetService("ReplicatedStorage").Damage:FindFirstChild(gt.Value))
local strings = tool.strings
BaseGun.No(hit, strings)
end
if game.ReplicatedStorage.Tals:FindFirstChild(gt.Value):FindFirstChild(tool.strings.T2.Value) then -- Talent 2
local TalFolder = game:GetService("ReplicatedStorage").Tals
local TalentMod = require(TalFolder:FindFirstChild(gt.Value):WaitForChild(tool.strings.T2.Value))
local strings = tool.strings
TalentMod.Yes(hit, strings, bullet, "T2")
end
if game.ReplicatedStorage.Tals:FindFirstChild(gt.Value):FindFirstChild(tool.strings.T3.Value) then -- Talent 3
local TalFolder = game:GetService("ReplicatedStorage").Tals
local TalentMod = require(TalFolder:FindFirstChild(gt.Value):WaitForChild(tool.strings.T3.Value))
local strings = tool.strings
TalentMod.Yes(hit, strings, bullet, "T3")
end
if game.ReplicatedStorage.Tals:FindFirstChild(gt.Value):FindFirstChild(tool.strings.T3.Value) then -- Talent 4
local TalFolder = game:GetService("ReplicatedStorage").Tals
local TalentMod = require(TalFolder:FindFirstChild(gt.Value):WaitForChild(tool.strings.T3.Value))
local strings = tool.strings
TalentMod.Yes(hit, strings, bullet, "T4")
end
end
delay(0, function()
bulletCache:ReturnPart(bullet)
end)
end
It looks like you can avoid nested conditionals here, and also cut down some redundancy by turning certain namespaces into variables.
Delay is converted to coroutine.wrap(f).
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local talents = ReplicatedStorage.Tals
local function onRayHit(cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
local talent = talents:FindFirstChild(gt.Value)
if not character then
return
end
if not character:FindFirstChild("Humanoid") then
return
end
if talent then
if talent:FindFirstChild(tool.strings.T1.Value) then -- Talent 1
local talentMod = require(talent:WaitForChild(tool.strings.T1.Value))
local strings = tool.strings
talentMod.Yes(hit, strings, bullet, "T1")
else
local BaseGun = require(ReplicatedStorage.Damage:FindFirstChild(gt.Value))
local strings = tool.strings
BaseGun.No(hit, strings)
end
if talent:FindFirstChild(tool.strings.T2.Value) then -- Talent 2
local talentMod = require(talent:WaitForChild(tool.strings.T2.Value))
local strings = tool.strings
talentMod.Yes(hit, strings, bullet, "T2")
end
if talent:FindFirstChild(tool.strings.T3.Value) then -- Talent 3
local talentMod = require(talent:WaitForChild(tool.strings.T3.Value))
local strings = tool.strings
talentMod.Yes(hit, strings, bullet, "T3")
end
if talent:FindFirstChild(tool.strings.T4.Value) then -- Talent 4
local talentMod = require(talent:WaitForChild(tool.strings.T4.Value))
local strings = tool.strings
talentMod.Yes(hit, strings, bullet, "T4")
end
end
coroutine.wrap(function()
bulletCache:ReturnPart(bullet)
end)()
end
Maybe shortening is not what you want, but increased readability.
Not sure what those T values are but this is what I would make of it:
local replicatedStorage = game:GetService("ReplicatedStorage")
local tals = replicatedStorage.Tals
local talents = tals[gt.Value]
local baseGun = replicatedStorage.Damage:WaitForChild(gt.Value))
local toolStrings = tool.strings
local talentValues = {
T1 = toolStrings.T1.Value,
T2 = toolStrings.T2.Value,
T3 = toolStrings.T3.Value,
T4 = toolStrings.T4.Value
}
-- I called it shoot, but I might be wrong
local function shoot(talentMod)
talentMod.Yes(hit, toolStrings, bullet, "T1")
end
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
for talentId, talentValue in ipairs(talentValues) do
local talentModule = talents:FindFirstChild(talentValue)
if talentModule then
local talent = require(talentModule)
talent.Yes(hit, toolStrings, bullet, talentId)
elseif talentId == "T1" then
BaseGun.No(hit, toolStrings)
end
end
end
-- I do not understand why you used a delay with 0 seconds here
bulletCache:ReturnPart(bullet)
end
This code is not tested. I noticed a lot of repeated logic, hence the for ipairs loop. Also, to @anon81993163 's credit, you cannot just shorten code when it is barely readable. It is necessary to use variables in order to understand what your code does. Only then can you think about making it more elegant and shorter.
For right here, what would i do if one of the talent values changed?
Also might of been on me, but what i normally was doing with
if game.ReplicatedStorage.Tals:FindFirstChild(gt.Value):FindFirstChild(tool.strings.T1.Value) then -- Talent 1
local TalFolder = game:GetService("ReplicatedStorage").Tals
local TalentMod = require(TalFolder:FindFirstChild(gt.Value):WaitForChild(tool.strings.T1.Value))
local strings = tool.strings
TalentMod.Yes(hit, strings, bullet, "T1")
else
local BaseGun = require(game:GetService("ReplicatedStorage").Damage:FindFirstChild(gt.Value))
local strings = tool.strings
BaseGun.No(hit, strings)
end
I was checking if there was a talent module with the same name as the value, if somehow someone got a talent that wasnt out or didnt have a module, and it was a tier 1 talent then it would just do the base damage
(All of the talent modules check what talent they are and they just do base damage w/ the talent)
Otherwise it looks good, ill check it out
(Edit 2)
Was spose to remove that at some point, was used for testing
(Edit 2)
Tried the code out and added some prints to debug sorta
local function onRayHit(Cast, result, velocity, bullet)
local hit = result.Instance
print(3)
local character = hit:FindFirstAncestorWhichIsA("Model")
print("a")
if character and character:FindFirstChild("Humanoid") then
print(2)
for talentId, talentValue in ipairs(talentValues) do
print("1")
local talentModule = talents:FindFirstChild(talentValue)
print(talentModule)
if talentModule then
print(4)
local talent = require(talentModule)
talent.Yes(hit, toolStrings, bullet, talentId)
print(talent)
elseif talentId == "T1" then
BaseGun.No(hit, toolStrings)
end
end
end
bulletCache:ReturnPart(bullet)
end
The prints dont go past 2, no matter what
Not sure whats wrong with it
(Edit)
Apparently you cant use ipairs in dictionaries so yeah
Okay pushed it a little but i got it shortened to this
local function onRayHit(Cast, result, velocity, bullet)
local Multi = 0
local Hit = result.Instance
local character = Hit:FindFirstAncestorWhichIsA("Model")
local T__ = tool.Talents:GetChildren()
local TalAmount = #T__
if character and character:FindFirstChild("Humanoid") then
for i = 1, TalAmount do
if TalentsMod[tool.Talents["T" .. i].Value] then
local D = TalentsMod[tool.Talents["T" .. i].Value](Hit, tool, bullet, "T" .. i)
if D ~= nil then
Multi += D
end
end
end
local DealDamage = require(game.ReplicatedStorage.MainModules.BaseDamage)
DealDamage[gt.Value](Hit, tool, Multi)
end
bulletCache:ReturnPart(bullet)
end
(Edit)
With this new system it allows the gun to have basically about any amount of talents, no longer just limited to 4