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" On Calling Remote Function In Script?

I'm getting this error message, "Unable to cast value to Object" when I try to call a Remote Function in a Script. I know I have to use a Local Script, but I can't seem to figure out how to call ".Touched" In a Local Script. It just doesn't work..

Script

local levelsHandler = require(game:GetService("ReplicatedStorage").LevelsHandler)

local circle = script.Parent.Parent.SpawnPad.Circle
local outline = script.Parent.Parent.SpawnPad.Outline

local isTouched = false

local function chooseRandom(dictionary)
	local list = {}

	for key, value in pairs(dictionary) do
		list[#list+1] = {key = key, value = value}
	end
	if #list == 0 then return end
	return list[math.random(#list)]
end

function OnTouch(part)
	local humanoid = part.Parent:WaitForChild("Humanoid")
	if (humanoid ~= nil) then
		if isTouched == false then
			isTouched = true

			circle.Color = Color3.fromRGB(38, 255, 0)
			outline.Color = Color3.fromRGB(38, 255, 0)

			local chosen = chooseRandom(levelsHandler)
			print("Selected Level: "..chosen.key)
			game.ReplicatedStorage.CreateLevel:InvokeClient(chosen.key) -- Error
		end
	end
end

script.Parent.Touched:Connect(OnTouch)

1 answer

heyyyy you need a player parameter in your invoke client!!!!!

https://developer.roblox.com/en-us/api-reference/function/RemoteFunction/InvokeClient

(my stuff above is cringe) here's the line you should replace (29th line):

local PlayerHit = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

game.ReplicatedStorage.CreateLevel:InvokeClient(PlayerHit, chosen.key)

7 comments

0

im not trying to be funny trust me im lazy to capitalize

0

oops too long lol

0

ok fixed the super long script

0

I dont have a player parameter in my StarterPlayer>StarterPlayerScripts>LocalScript and that one works fine

0

Also, I tried this and it didn't work. I'm not getting any error messages though. My Print/Console messages are going through as well. Very Strang...

0

oh hey you used localscript, then use localplayer, if you are using normal script use that way

0

what's your new script? can you update it on your question or post another

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