I’ve made some sorcery scripts, allowing the user to execute 2 attacks: Fireball and Heatwave.
I wanted to know what i did of good and of bad regarding the scripts, and how to fix any issues with it.
I’m fascinated with this sort of thing since ever
There are 4 scripts in total:
LocalScript “FireSpellcast” inside of StarterPlayerScripts
ServerScript “FireSpells” inside of ServerScriptService
ModuleScript “Spells” inside of FireSpells
ServerScript “TouchManager” inside of the projectiles, inside of ServerStorage
FireSpellcast
CAS = game:GetService("ContextActionService")
Player = game.Players.LocalPlayer
Character = Player.Character or Player.CharacterAdded:Wait()
Humanoid = Character:WaitForChild("Humanoid")
HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
FireballCastingAnim = Humanoid:LoadAnimation(script.FireballCasting)
HeatwaveCastingAnim = Humanoid:LoadAnimation(script.HeatwaveCasting)
function Fireball()
FireballCastingAnim:Play()
delay(0.5, function()
game.ReplicatedStorage.SpellRemotes.Fireball:FireServer(HumanoidRootPart)
end)
CAS:UnbindAction("Fireball")
wait(2)
print("Fireball Ready")
CAS:BindAction("Fireball",Fireball,false,Enum.KeyCode.E)
end
function Heatwave()
HeatwaveCastingAnim:Play()
delay(0.8,function()
game.ReplicatedStorage.SpellRemotes.Heatwave:FireServer(HumanoidRootPart)
end)
CAS:UnbindAction("Heatwave")
wait(4)
print("Heatwave Ready")
CAS:BindAction("Heatwave",Heatwave,false,Enum.KeyCode.Q)
end
CAS:BindAction("Fireball",Fireball,false,Enum.KeyCode.E)
CAS:BindAction("Heatwave",Heatwave,false,Enum.KeyCode.Q)
FireSpells
Spells = require(script:WaitForChild("Spells"))
game.ReplicatedStorage.SpellRemotes.Fireball.OnServerEvent:Connect(Spells.Fireball)
game.ReplicatedStorage.SpellRemotes.Heatwave.OnServerEvent:Connect(Spells.Heatwave)
Spells
local Spells = {}
local SpellObjects = {
Fireball = game.ServerStorage.FireProjectiles.Fireball,
Heatwave = game.ServerStorage.FireProjectiles.Heatwave
}
function Spells.Fireball(Player,HRP) -- Direct attack with high damage and small hitbox
local StartPos = HRP.CFrame:ToWorldSpace(CFrame.new(0,0,-2))
local Projectile = SpellObjects.Fireball:Clone()
local Vel = Projectile:WaitForChild("Vel")
local Fire = Projectile:WaitForChild("Fire")
local TouchManager = Projectile:WaitForChild("TouchManager")
Projectile.Parent = workspace:WaitForChild("Projectiles")
Projectile.CFrame = StartPos
Vel.Velocity = Projectile.CFrame.lookVector*30
Fire.Enabled = true
TouchManager.Disabled = not true
delay(5,function()
Projectile:Destroy()
end)
TouchManager.OnTouched.Event:Connect(function(Hit)
if not Hit:IsDescendantOf(Player.Character) then
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
if Humanoid.Health > 0 then
Humanoid:TakeDamage(34)
Projectile:Destroy()
end
end
end
end)
end
function Spells.Heatwave(Player,HRP) -- AOE attack with low damage and speed with a large hitbox
local StartPos = HRP.CFrame:ToWorldSpace(CFrame.new(0,0,-2))
local Wave = SpellObjects.Heatwave:Clone()
local Vel = Wave:WaitForChild("Vel")
local Fire = Wave:WaitForChild("Fire")
local TouchManager = Wave:WaitForChild("TouchManager")
local AlreadyHitTargets = {}
Wave.CFrame = StartPos
Wave.Parent = workspace.Projectiles
Vel.Velocity = Wave.CFrame.lookVector*15 - Vector3.new(0,1,0)
Fire.Enabled = true
TouchManager.Disabled = not true
delay(5,function()
Wave:Destroy()
for Index, Value in ipairs(AlreadyHitTargets) do
table.remove(AlreadyHitTargets,Index)
end
end)
TouchManager.OnTouched.Event:Connect(function(Hit) -- check if its not player, if its human and whether or not it has already been damaged before
if not Hit:IsDescendantOf(Player.Character) then
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
local EnemyCharacter = table.find(AlreadyHitTargets,Humanoid.Parent)
if not EnemyCharacter then
if Humanoid.Health > 0 then
table.insert(AlreadyHitTargets,#AlreadyHitTargets+1,Humanoid.Parent)
Humanoid:TakeDamage(20)
end
end
end
end
end)
end
return Spells
TouchManager
script.Parent.Touched:Connect(function(Hit)
script.OnTouched:Fire(Hit)
end)
My first post in Code Review!!!