"Unable To Cast Value To Object" Help

Hello! I currently have a problem where everytime i run my script, i get this error output for the script

  16:05:02.488  Unable to cast value to Object  -  Server - Script:14

and heres the script:

local sword = script.Parent.Handle
sworddamage = 50
script.Parent.Equipped:Connect(function()
	local eq = true

sword.Touched:Connect(function(hit)
if eq == true then
	if game.Players:FindFirstChild(hit.Parent.Name) or hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Humanoid.Health <= sworddamage or hit.Parent.Humanoid.Health >= 0 then
local remoteEvent = game.ReplicatedStorage:WaitForChild("Dead")
local deadperson = hit.Parent.Name
local killer = script.Parent.Parent.Name

		remoteEvent:FireClient(deadperson, killer)
			print("rip")	
			end
		end
	end
end)
end)

this script goes inside of a sword, this script is supposed to be a death effect manager,
but I’m currently testing it to see if I could use remote events…

I’ve tried everything I know to solve this issue but at last I wasn’t able todo anything… please feel free to comment any questions you might have…

That’s because you’re trying to pass a string to the first argument of :FireClient which should be a player:

local players = game:GetService('Players')
-- ...
local deadperson = players:GetPlayerFromCharacter(hit.Parent)
local killer = script.Parent.Parent.Name

remoteEvent:FireClient(deadperson, killer)

Ooooohhhh so the first perameter of an remote event has to be a actual player? if so then the “dead person” in this case is actually an npc

Oh I see, just check if the deadperson is an actual player or not, then fire the client depending on whether the dead “person” is an actual player or not:

local deadperson = players:GetPlayerFromCharacter(hit.Parent)
local killer = script.Parent.Parent.Name
if deadperson then
    remoteEvent:FireClient(deadperson, killer)
end

yeah, the “deadperson” in this case was an NPC in workspace… is there no way to actually be able to use an npc in this case? or if the perameters do require a player to be first then maybe i could try: remoteEvent:FireClient(killer, deadperson) ?

There isn’t any way since there is no player, there is no client to fire.

wait, there is a player… the “killer” is supposed to be the actual player that killed the npc, if that helped make sense

Then you should be able to just use the if deadperson then thing

ok, so i have another code i wanna show you…

local sword = script.Parent.Handle
sworddamage = 50
script.Parent.Equipped:Connect(function()
	local eq = true

sword.Touched:Connect(function(hit)
if eq == true then
	if game.Players:FindFirstChild(hit.Parent.Name) or hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Humanoid.Health <= sworddamage or hit.Parent.Humanoid.Health >= 0 then
local remoteEvent = game.ReplicatedStorage:WaitForChild("Dead")
local deadperson = hit.Parent
local killer = game.Players["5566ghosty"]

		remoteEvent:FireClient(killer, deadperson)
			print("rip")	
			end
		end
	end
end)
end)

i will use your’s and test it out but i wanna se if this is a little bit more of a improvment… i dont get an error this time.
but the script i have in serverscriptservice wont print the thing…
the serverscriptservice script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("Dead")
print("Part 1 of remote control Is Working")

remoteEvent.OnServerEvent:Connect(function(killer, deadperson)

print("Part 2 of remote control Is Working")
  print(killer.Name, deadperson.Name)
end)

btw the print("Part 2 of remote control Is Working") wont print

Hmm, I’m a bit confused, are you trying to fire another client’s remote from your own weapon? Or are you trying to get a signal from the server?

Basically is the script in your weapon a local script or server script?

1, the script thats firing the remote is in a tool (Server Script), The thing that is getting the remote signal is in “ServerScriptService”
2, yeah the script in the weapon is a Server Script

Ahh okay, try using a BindableEvent instead, remote events/functions are used to handle passing information from the client to the server and vice versa whereas bindable events/functions are used to pass information on the same client/server boundary

Do Bindable events have the same setup as remote events? or is there another process?

Essentially the same, yeah. Just switch :FireClient to :Fire, and switch .OnServerEvent with .Event

Omg thanks LOLLLLLL, this took me longer then expected. I’m surprised you actually put up with my confusing nonsense, But anyways thanks for all your helpppppp :}}}}}}

1 Like