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

RemoteFunction:InvokeClient() Error, Can Anyone Help?

Asked by 4 years ago

Hello everyone, this is my first post! :D I am trying to code a door that when a player touches the door, they need to hit the 'E' key to open the door.

When a Player touches a part called 'Center', It should trigger a RemoteFunction that Invokes the Client to run a contextActionService to check for the 'E' key. If pressed, then the RemoteFunction returns true and runs code to open the door. Instead I get an error on the twelfth line saying "Unable to cast value to Object". I know it has something to do with the parameters being passed through the RemoteFunction:InvokeClient but I don't know what I'm doing wrong, can anyone help me?

  • This is in a script
local Door = script.Parent
local doorTouched = false
local result = false
local RemoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction")

Door.Center.Touched:connect(function(hit)
    if not doorTouched then
        doorTouched = true
        print("You Touched It!")
        local player = hit.Parent.Name
        result = RemoteFunction:InvokeClient(player, doorTouched)
        if result == true then
            print("Door Open")
        end
        wait(1)
        doorTouched = false
    end 
end)
  • This is in a local script
local player = game.Players.LocalPlayer
local contextActionService = game:GetService("ContextActionService")

game.ReplicatedStorage.RemoteFunction.OnClientInvoke = function(doorTouched)
    if doorTouched == true then
        local function onKeyPress(actionName, userInputState, inputObject)
            if userInputState == Enum.UserInputState.Begin then
                print("E was Pressed") 
                return true
            end
        end 
        contextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.E)
    else return false
    end
end 

Answer this question