"Unable to cast value to Object" error message

Viewed 1838

So I am using a remote function, as seen at the end, and for some reason, a normal assignment of a variable won't work, it is giving me the error message, "Unable to cast value to Object," what is wrong?

local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function (hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        storeEvent:InvokeClient(slotNum)
    end
end)

and connecting to the other script:

script.Parent.OnInvoke:Connect(function (slot)
    local StoreArrows = game.ReplicatedStorage.StoreArrows
    StoreArrows.SlotNum.Value = slot
    local cam = game.Workspace.Camera
    local storeButtons = script.Parent
    local camNum = game.ReplicatedStorage.StoreArrows.CamNum.Value
    local camNumInst = game.Workspace.CamStorage:WaitForChild("Cam-"..camNum)
    cam.CameraType = Enum.CameraType.Scriptable
    cam.CFrame = camNumInst.CFrame
    local clonedStoreButtons = StoreArrows:Clone()
    clonedStoreButtons.Parent = player.PlayerGui.ScreenGui
end)
1 Answers

Keep in mind that many clients connect to a server at a time. So when you call a RemoteEvent's InvokeClient function, you have to tell it which client to invoke it on. The first parameter to InvokeClient is supposed to be the player, that's why the error is telling you that it cannot cast the slotNum value to a player object.

local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function(hit)
    -- check that the thing that we touched is actually a player
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        -- tell that player to open the store
        storeEvent:InvokeClient(player, slotNum)
    end
end)
Related