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)
You should use :FireClient() to fire a event to a client or :FireServer() to fire a event to the whole server.
I'll make a little example of how to use it(with :FireClient())
-- server script local event -- type here the location of the RemoteEvent local ClickDetector = workspace.Part.ClickDetector ClickDetector.MouseClick:Connect(function(plr) event:FireClient(plr) -- you will always need the client as the first parameter when firing a event from the server because the event needs to know to what player it should fire the event(you will not need to do this with :FireServer()) end) -- local script local event local plr = game:GetService('Players').LocalPlayer event.OnClientEvent:Connect(function() -- you will not need the player parameter here because you can get the player with LocalPlayer print('hello from the server to the client '.. plr.Name..’!') end)