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

"Unable to cast value to Object" On Calling Remote Function In Script?

Asked by 3 years ago

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

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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)
0
im not trying to be funny trust me im lazy to capitalize Xapelize 2658 — 3y
0
oops too long lol Xapelize 2658 — 3y
0
ok fixed the super long script Xapelize 2658 — 3y
0
I dont have a player parameter in my StarterPlayer>StarterPlayerScripts>LocalScript and that one works fine EightBlits 7 — 3y
View all comments (3 more)
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... EightBlits 7 — 3y
0
oh hey you used localscript, then use localplayer, if you are using normal script use that way Xapelize 2658 — 3y
0
what's your new script? can you update it on your question or post another Xapelize 2658 — 3y
Ad

Answer this question