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

How do I set a remote function variable?

Asked by 8 years ago

I'm wondering if this is set up correctly? The functionality of this script would be when a user pushes a button, they're kicked from the server. I know you can do this easily another way, I just need help with remote functions.

--In LocalScript
local player = game.Players.LocalPlayer.Name
script.Parent.MouseButton1Down:connect(function() -- Lets say the scripts in a textbutton
game.Workspace.RemoteFunction:InvokeServer(player)
end)

--In ServerScript
local rf = Instance.new("RemoteFunction")
rf.Parent = game.Workspace
rf.Name = "RemoteFunction"
function rf.OnServerInvoke(player)
    game.Players[player]:Kick("You kicked yourself")
end

1 answer

Log in to vote
2
Answered by
LuaQuest 450 Moderation Voter
8 years ago

Okay, a few things wrong here.

1: You cannot call "OnServerInvoke" from a client. That defeats the purpose of it having server-to-client communication.

2: You don't need a variable for the client.

3: The first argument of InvokeServer is the client by default, so we don't need to return any values.

Try this:

-- This is inside a server script
local rf = Instance.new("RemoteFunction",workspace)

-- The function should be created in a server script environment.
function rf.OnServerInvoke(client)
    client:Kick("You've been kicked")
end

-- This is inside a local script
repeat wait() until workspace:FindFirstChild("RemoteFunction") -- just incase it hasn't loaded yet
local rf = workspace.RemoteFunction

script.Parent.MouseButton1Down:connect(function()
    rf:InvokeServer()
end

-- You will now find yourself being kicked when you press the GUI button.
0
For lines 10 and 11, you could just do local rf = workspace:WaitForChild("RemoteFunction"). Also, I'd use a RemoteEvent because you're not returning anything. funyun 958 — 8y
0
Thank you for answering, but I just wanted to know how (if possible) to carry a variable from the client script over to the server script. Kicking the client isn't the purpose of the actual script I'm creating. magiccube3 115 — 8y
0
Actually, you DO need OnServerInvoke on the client if you're expecting information being sent to the client from the invocation of the server (OnClientInvoke for client to server). With what the asker said, that client variable was warranted, whatever the first argument of that function is it will always be the player value. M39a9am3R 3210 — 8y
Ad

Answer this question