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

"Unable to cast value to object"?

Hi.

I have a weird phone that you can use to dial weird numbers and affect the environment (don't judge me).

But the problem I'm encountering is when it's dialed, it's supposed to make a message in the chat: "Player has dialed number 1800LOLFAKE -- and it was valid! Something happened!" or "Player has dialed number 1800LOLSTOP, but it wasn't valid, so nothing happened.", depending on whether the number is valid or not.

It doesn't do that, though. Instead, it returns an error:

Unable to cast value to Object Stack Begin Script 'Workspace.Phone.Dial.Script', Line 14 Stack End

Here's the script that doesn't work:

-- Get Events
local event = game:GetService("ServerStorage").Event
local dial = game:GetService("ReplicatedStorage").DialNotify
-- Get Input
local input = script.Parent.Parent.Input
-- Get ClickDetector
local click = script.Parent.ClickDetector
-- Make validity
local valid;

click.MouseClick:connect(function(p) -- c l i c k   c l i c k
	if input.Value == "666" then
		valid = true
		event:Fire("Spook"); dial:FireClient(p.Name, input.Value, valid)
	elseif input.Value == "777" then
		valid = true
		event:Fire("Happy"); dial:FireClient(p.Name, input.Value, valid)
	else
		valid = false
		dial:FireClient(p.Name, input.Value, valid)
	end
	input.Value = "" -- Delete input
end)

Why is this happening, and how can I fix it?

kthxbye

1 comment

0

FireClient requires the player object not the player name which is a string. Just use p since this holds the player object.

1 answer

✓ Accepted answer

The error is caused by the :FireClient() function

This is how FireClient is used: Event:FireClient(PlayerObject, Argument, Argument)

What you are doing is you're sending the player's name instead of the object.

The correct form of this should be: :FireClient(p) not :FireClient(p.Name)

2 comments

0

The three arguments I'm sending now are just the player object, the input object, and the valid bool, and now it won't even throw an error... :/

0

Then the other one is a whole different problem, use print() to track down where the script stops.

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