This script is supposed to get the player inside and deal damage to them, but also makes them flinch. I'm trying to use a RemoteEvent to tell the player that's being damaged to start the flinch animation.
My script errors and says: Unable to cast value to object I am also using a pcall in case it does error which it did.
Server Script
local replicatedStorage = game:GetService("ReplicatedStorage")
local combatEvent = replicatedStorage:WaitForChild("combat")
local dmg = 5
local hitboxSize = 2
combatEvent.OnServerEvent:Connect(function(player, action, V1)
local character = player.Character
if action == "combat" then
local Region = Region3.new(V1 - Vector3.new(hitboxSize,hitboxSize,hitboxSize),V1 + Vector3.new(hitboxSize,hitboxSize,hitboxSize))
local RTable = workspace:FindPartsInRegion3(Region, nil, 100)
local prt = Instance.new("Part", game.Workspace)
prt.Anchored = true
prt.CanCollide = false
prt.CFrame = Region.CFrame
prt.Size = Region.Size
game.Debris:AddItem(prt,1)
local Deb = "Deb"
local Humanoid = "Humanoid"
print("Running")
for i,v in pairs(RTable) do
if v.Parent:findFirstChild(Humanoid) and v.Parent:findFirstChild(Deb) == nil and v.Parent ~= character then
print("Hit")
local Deb = Instance.new("BoolValue", v.Parent)
Deb.Name = tostring(Deb)
game.Debris:AddItem(Deb,.2)
local enemyName = v.Parent.Name
local enemyCombat = v.Parent:GetChildren()
print(enemyName)
local success, response = pcall(combatEvent.FireClient, combatEvent, "Player1")
print(success, " - Error: ",response)
v.Parent.Humanoid:TakeDamage(dmg)
break
end
end
end
end)
Local Script
local replicatedStorage = game:GetService("ReplicatedStorage")
local combatEvent = replicatedStorage:WaitForChild("combat")
local canAttack = true
local function flinch()
canAttack = false
print("I should be flinching ")
canAttack = true
end
combatEvent.OnClientEvent:Connect(flinch)

1 comment
You might be asleep right now, but could you tell me which line the error occurs on?