Relatively beginner scripter, looking for pointers , I feel as if my script sucks, or atleast is flawed or in someway unoptimized or being executed in a bad way with better alternatives overall. Looking for someone with more experience who can confirm or deny with some tips,
--Client Module Script
local Knife = {}
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
--//Requires
local AnimationPlayer = require(script.Parent:WaitForChild("AnimationPlayer"))
--//Variables
local events = ReplicatedStorage:WaitForChild("Events")
local requestDamage = events:WaitForChild("Damage")
local lplayer = game.Players.LocalPlayer
local character = lplayer.Character or lplayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local params = OverlapParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {character}
--Everytime tool is activated
local function activateTool(tool)
if AnimationPlayer.isSwinging or AnimationPlayer.isThrowing then return end
local hitbox = tool:WaitForChild("Hitbox")
local startTime = os.clock()
local hitCharacters = {}
task.spawn(AnimationPlayer.swingKnife, tool)
while AnimationPlayer.isSwinging do
if not tool:FindFirstChild("Hitbox") then break end
local parts = workspace:GetPartsInPart(hitbox, params)
for _, part in pairs(parts) do
if part.Parent:FindFirstChild("Humanoid") then
local char = part.Parent
local humanoid = char:FindFirstChild("Humanoid")
if humanoid and humanoid.Health > 0 and not table.find(hitCharacters, char) then
table.insert(hitCharacters, char)
if #hitCharacters > 2 then return end
requestDamage:FireServer(char) -- server damages
end
end
end
task.wait()
end
hitCharacters = {}
end
--When user tries to throw knife
local function knifeThrow(inputObj, isTyping)
end
UserInputService.InputBegan:Connect(knifeThrow)
--Setup a knife tool when added to player/char
local function detectTool(tool)
if tool:IsA("Tool") and tool:GetAttribute("Type") == "Knife" then
if not tool:GetAttribute("ActivatedConnection") then
tool:SetAttribute("ActivatedConnection", true)
tool.Activated:Connect(function()
activateTool(tool)
end)
end
end
end
lplayer.Backpack.ChildAdded:Connect(detectTool)
character.ChildAdded:Connect(detectTool)
--Setup the character on respawn
local function setupCharacter(char)
character = char
humanoid = char:WaitForChild("Humanoid")
params.FilterDescendantsInstances = {character}
character.ChildAdded:Connect(detectTool)
end
lplayer.CharacterAdded:Connect(setupCharacter)
return Knife
--Client Module Script
local AnimationPlayer = {}
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local ContentProvider = game:GetService("ContentProvider")
--//Requires
local itemsList = require(ReplicatedStorage:WaitForChild("Dictionary"):WaitForChild("ItemsList"))
--//Variables
local animations = ReplicatedStorage:WaitForChild("Animations")
local lplayer = game.Players.LocalPlayer
local character = lplayer.Character or lplayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
--Swinging knife animation combo
local currentTrack = 0
function AnimationPlayer.swingKnife(tool)
if AnimationPlayer.isSwinging then return end
AnimationPlayer.isSwinging = true
currentTrack += 1
local animationPath = itemsList[tool.Name].Animations.Slashes
if currentTrack > #animationPath:GetChildren() then currentTrack = 1 end
local anims = animationPath:GetChildren()
local clone = anims[currentTrack]:Clone()
local track = animator:LoadAnimation(clone)
track:Play()
track.Stopped:Wait()
task.wait(0.1)
AnimationPlayer.isSwinging = false
Debris:AddItem(clone,0.25)
end
-- Throwing knife animation
function AnimationPlayer.throwKnife(tool)
end
--Setup the character on respawn
local function setupCharacter(char)
character = char
humanoid = char:WaitForChild("Humanoid")
animator = humanoid:WaitForChild("Animator")
AnimationPlayer.isSwinging = false
AnimationPlayer.isThrowing = false
end
lplayer.CharacterAdded:Connect(setupCharacter)
--Preload all animations (TEMPORARY)
print("Loading")
for _,mainFolder in pairs(animations:GetChildren()) do
for _,folder in pairs(mainFolder:GetChildren()) do
for _,anims in pairs(folder:GetChildren()) do
ContentProvider:PreloadAsync({anims})
end
end
end
print("Loaded all assets")
return AnimationPlayer