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

I have problems with "unable to cast value to object". What to do?

Hi i had a question because this is the first time i'm using FireClient/FireServer and RemoteEvents.

From what i know FireServer can only be used with a localscript and FireClient can be used on a script (Correct me if i'm wrong).

So i wanted to make a RemoteEvent when i rank up(when the rank value changes) that there will be some Fireworks! But it won't work on the line with:

rank.Changed:connect(function()
	onRankChanged(plr)
end)

everytime it says "unable to cast value to object" and i can't fix it so if you can please say what i need to do :) and what i did wrong!

Thanks! (P.S. I won't give that much of the code because it's something i don't realy want to sare if you know what i mean :P)

--Script

rank.Changed:connect(function()
	onRankChanged(plr)
end)

function onRankChanged(plr)
	local leaderstats = game.Players:FindFirstChild(plr.Name):findFirstChild("leaderstats")
	if (leaderstats ~= nil) then
		local rank = leaderstats:FindFirstChild("Rank")
		if plr ~= nil then
			if game.Players:FindFirstChild(plr.Name) ~= nil then
				if (rank ~= nil) and (leaderstats ~= nil) then
					rEvents:FindFirstChild("FireworkEvent"):FireClient(game.Players:FindFirstChild(plr.Name),"Rank Up!")
				end
			end
		end
	end
end
--LocalScript

local FWEvent = game.ReplicatedStorage.Events.FireworkEvent

FWEvent.OnClientEvent:connect(function()
	Fireworks(12)
end)

6 comments

0

What line does the error appear on?

0

on line 4 in the "Script"

0

That means that the error probably appears in the function. Is that the entire error output?

0

yes

0

That error normally appears when you call a function with the wrong type of argument. What type of value is "plr"?

0

"plr" is a value from a PlayerAdded function andline4 is in that function because of leaderboard

1 answer

rank.Changed:connect(function(plr) -- You need to set player as a received argument.
    onRankChanged(plr)
end)

1 comment

0

The only paramater for "Changed" is the property that was changed. "plr" is in no way a reference to the local player object.

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