Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Unable to cast value to object- Remote Event?

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)
0
Didnt work ewdoggypoopoo 12 — 4y
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. EmilyBendsSpace 1025 — 4y
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. EmilyBendsSpace 1025 — 4y
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. EmilyBendsSpace 1025 — 4y
Ad

Answer this question