Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Unable to cast value to object- Remote Event?

Code in a script that's inside of a tool; I have a RemoteEvent thats in the script and when the player equips the tool it copies the RE and puts it into game.ReplicatedStorage with a different name. Now I need help somehow firing the Remote Event.

script.Parent.Equipped:Connect(function()
	local player = script.Parent.Parent.Name
	local RE = script.WeaponEquipped:Clone()
	RE.Name = ("Equipped" .. player)
	RE.Parent = game.ReplicatedStorage
	wait(1)
	game.ReplicatedStorage:FindFirstChild("Equipped" .. player):FireClient(player)
end)

Output comes back with "Unable to cast value to object, line 7"

1 answer

I mean, a few things.

First of all, you already store your new RemoteEvent as your "RE" variable, so there's no need to access it with FindFirstChild

Two, I don't see a point to you cloning the remote, but whatever floats your code.

And finally three, you're passing the player's name as the first argument to FireClient, which must be the player object!

Here's a way to do it, though by no means the best one. It depends.

--NOTE: Just an example, YMMV if you try to use this without modifications.
local player = script.Parent.Parent
local RE = script.WeaponEquipped:Clone()
RE.Name = ("Equipped" .. player.Name)
RE.Parent = game.ReplicatedStorage

script.Parent.Equipped:Connect(function()
	wait(1) --Unnecessary wait?
	RE:FireClient(player)
end)

4 comments

0

Didnt work

0

Yeah, it wouldn't, because script.Parent.Parent being assigned to player is not a Player object, it's a character Model. You need to use Players:GetPlayerFromCharacter(script.Parent.Parent) to get the Player object.

0

Also, cloning the remote event is a very bad idea. The server Script and client LocalScript should reference the one that's the child of the script. It's already a copy. By cloning it and not giving it a unique name, you risk creating multiple copies of the event with the same name in ReplicatedStorage, and then client and server won't be referencing the same one except sometimes by random chance.

0

If you want one copy of the event in ReplicatedStorage, you have to add code to ensure that the server does not make a second copy of an event that already exists in there with the same name.

This discussion is preserved as it was. New answers, comments, and votes are closed.