Hey devforum! How would I pass a function as an argument?
Here’s my code:
LocalScript:
-- Don't make the keycode F or else it will interfere with the flashlight
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local Player = Players.LocalPlayer
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local InteractionSystemEevent = RemoteEvents:WaitForChild("InteractionSystemEvent")
local Mouse = Player:GetMouse()
local SelectionBox = script:WaitForChild("SelectionBox")
function MouseMoved()
ContextActionService:UnbindAction(Player.Name.."Interaction")
if Mouse.Target then
local Object = Mouse.Target
if Object:HasTag("CanInteract") then
local InteractionInstructions = require(Object:WaitForChild("InteractionInstructions"))
local Keycode = InteractionInstructions.Keycode
local RunContext = InteractionInstructions.RunContext
local function HandleAction(actionName, inputState, _inputObject)
if actionName == Player.Name.."Interaction" and inputState == Enum.UserInputState.Begin then
if RunContext == Enum.RunContext.Server then
InteractionSystemEevent:FireServer(InteractionInstructions.InstructionsToCarryOut)
elseif RunContext == Enum.RunContext.Client then
InteractionInstructions.InstructionsToCarryOut()
else
warn(Object.Name..".InteractionInstructions.RunContext is invalid: Must be Enum.RunContext.Server or Enum.RunContext.Client")
end
SelectionBox.Parent = script
SelectionBox.Adornee = nil
end
end
SelectionBox.Parent = workspace
SelectionBox.Adornee = Object
ContextActionService:BindAction(Player.Name.."Interaction",
HandleAction,
true,
Keycode)
elseif Mouse.Target == nil then
SelectionBox.Parent = script
SelectionBox.Adornee = nil
ContextActionService:UnbindAction(Player.Name.."Interaction")
else
SelectionBox.Parent = script
SelectionBox.Adornee = nil
end
end
end
Mouse.Move:Connect(MouseMoved)
ServerScript:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local InteractionSystemEevent = RemoteEvents:WaitForChild("InteractionSystemEvent")
function OnServerEvent(player, ...)
({...})[1]()
end
InteractionSystemEevent.OnServerEvent:Connect(OnServerEvent)
I apologize if the code lacks comments or documentation as this project of mine is in it’s very early stages.
The InstructionsToCarryOut()
function is a function without any arguments located inside of a ModuleScript in any part or model with the CanInteract tag that should be ran. Inside of the ModuleScript is also a RunContext
variable which should decide whether it should be ran on the client or server to prevent using many RemoteEevents. There are 3 conditions which decide so and are located in the LocalScript. If the RunContext
is set to the server then I want the InstructionsToCarryOut()
function to be ran in the ServerScript using the RemoteEvent. I’m using a tuple and it doesn’t seem to be working. I keep getting a attempt to call a nil value
error on Line 7 of the ServerScript.
Any help much appreciated!
Thank you in Advance.