So i was using a tutorial on how to make a grab skill in youtube and everything was going well until i started testing. The problem is that it attempts to index nil with CFrame in line 34 of the Module Script. I looked around other topics but they dont solve my issue.
Module Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Animations = ReplicatedStorage.Animations
local Handler = {}
Handler.playerDebounce = {}
Handler.hitboxLifeTime = .25
Handler.grabDuration = 1
Handler.tickRate = 10
Handler.damage = 1
Handler.confirmTarget = {}
Handler.coolDown = 1
function Handler.playAnimations(humanoid: Humanoid, typeOfAnim: string)
local currentAnim
if typeOfAnim == "User" then
currentAnim = humanoid.Animator:LoadAnimation(Animations.GrabUser)
elseif typeOfAnim == "Target" then
currentAnim = humanoid.Animator:LoadAnimation(Animations.GrabTarget)
end
return currentAnim
end
function Handler.spawnHitbox(player, character, rootPart)
local Folder = Instance.new("Folder")
Folder.Name = "GrabMove"
Folder.Parent = workspace
local Hitbox = ReplicatedStorage.Hitbox:Clone()
Hitbox.CFrame = rootPart.CFrame * CFrame.new(0, 0, -Hitbox.Size.Z) --Line 34 Heres the problem
Hitbox.Parent = Folder
local weld = Instance.new("Weld")
weld.Part0 = Hitbox
weld.Part1 = rootPart
weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
weld.Parent = weld.Part0
local connection = Hitbox.Touched:Connect(function()end)
local results = Hitbox:GetTouchingParts()
coroutine.wrap(function()
for i=1, #results do
local obj = results[i]
if not obj:IsDescendantOf(character) then
local enemyHumanoid = obj.Parent:FindFirstChild("Humanoid")
local enemyRoot = obj.Parent:FindFirstChild("HumanoidRootPart")
if enemyHumanoid and enemyRoot and enemyHumanoid.Health > 0 then
if Handler.confirmTarget[player.Name] then
return
end
Handler.confirmTarget[player.Name] = true
local newCframe = rootPart.CFrame * CFrame.new(0, 0, -4)
local lookPosition = Vector3.new(rootPart.Position.X, enemyRoot.Position.Y, rootPart.Position.Z)
enemyRoot.CFrame = CFrame.lookAt(newCframe.Position, lookPosition)
local targetAnimation = Handler.playAnimations(enemyHumanoid, "Target")
targetAnimation:Play()
coroutine.wrap(function()
local canTick = true
task.delay(Handler.hitboxLifeTime, function()
canTick = false
end)
while task.wait(.1) do
if not canTick then
break
end
enemyHumanoid:TakeDamage(Handler.damage)
task.wait(Handler.grabDuration / Handler.tickRate)
end
end)()
if Hitbox then
Folder:Destroy()
end
if connection then
connection:Disconnect()
end
return
end
end
end
end)()
task.delay(Handler.hitboxLifeTime, function()
if Hitbox then
Folder:Destroy()
end
if connection then
connection:Disconnect()
end
end)
end
return Handler
Server Script which is the parent of the Module Script it may help:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GrabRemote = ReplicatedStorage:WaitForChild("Grab")
local Handler = require(script.Handler)
GrabRemote.OnServerEvent:Connect(function(player)
local character = player.Character
local humanoid = character.Humanoid
local rootPart = character.HumanoidRootPart
if Handler.playerDebounce[player.Name] then
return
end
Handler.playerDebounce[player.Name] = true
local userAnimation = Handler.playAnimations(humanoid, "User")
userAnimation:Play()
userAnimation:GetMarkerReachedSignal("Damage"):Connect(function()
Handler.spawnHitbox(rootPart)
task.delay(Handler.hitboxLifeTime, function()
if not Handler.confirmTarget[player.Name] then
userAnimation:Stop()
task.delay(Handler.coolDown, function()
if Handler.playerDebounce[player.Name] then
Handler.playerDebounce[player.Name] = nil
end
if Handler.confirmTarget[player.Name] then
Handler.confirmTarget[player.Name] = nil
end
end)
GrabRemote:FireClient(player, Handler.coolDown)
else
task.delay(Handler.grabDuration, function()
if Handler.playerDebounce[player.Name] then
Handler.playerDebounce[player.Name] = nil
end
if Handler.confirmTarget[player.Name] then
Handler.confirmTarget[player.Name] = nil
end
GrabRemote:FireClient(player, Handler.coolDown)
end)
end
end)
Handler.spawnHitbox(player, character, rootPart)
end)
end)