Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can I invoke a client and server within the same Remote Function?

Hello scripters, I have a remote function that triggers a screen GUI to pop up when a brick is clicked. In return, the GUI has a button, theoretically what I want to do is send a bool value back to the remote function if the person clicks the GUI button. Any thoughts ideas? (I'm trying not to clutter my place with multiple remote events/functions)

I am pretty new to using remote events/functions just as a side note, so please excuse the scripting.

Server script:

local Mainevent= Instance.new("RemoteFunction")
Mainevent.Parent = workspace
Mainevent.Name = "Acceptance"

local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = game.workspace.eventpart
clickDetector.MouseClick:connect(function(player)
	if not game.workspace.Events:FindFirstChild('Event') then
		local fold = Instance.new("Folder",game.Workspace.Events)
		fold.Name = "Event"
		for i,v in pairs(game.Players:GetChildren()) do 
			wait()
			event:InvokeClient(v)
		end
	end
end)

function workspace.Acceptance.OnServerInvoke(player, bool)
	if bool== true then
		print("True")
	end
end

Client Script:

function workspace.Acceptance.OnClientInvoke()
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true
	print("EventHappened")
end

function yes()
	return true
end

game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.TextButton.MouseButton1Down:connect(yes)

1 answer

Yes, you can do all of your game's functions through a RemoteFunction (just note that the server won't be able to fire it on the server, and vice versa).


LocalScript:

--// Defining things earlier on for ease of use, and optimization
local event = game.ReplicatedStorage:WaitForChild("Acceptance")
local frame = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Frame")

function event.OnClientInvoke()
	frame.Visible = true
	print("Event Happened")
end

function send()
	event:InvokeServer(true)
end

--// We only use TextButton once, so need to define it earlier in the script. I also renamed a few things, to be more understandable and to work better.
frame:WaitForChild("TextButton").MouseButton1Click:connect(send)

Tip 1:

Use Tab to indent your code, not Space. Space is inconsistent and very annoying.

Tip 2:

For faster, more efficient and easier to write code, define things earlier in the script, so that you don't need to write long strings of GrandParent:WaitForChild("Parent"):WaitForChild("Child") every time you want to index an object.

Make sure to optimise your script on the server, too.

Tip 3:

Use :WaitForChild() because things may not have loaded when the code runs, therefore returning an error and stopping your code).

TL;DR - use tab, don't index things twice, and use :WaitForChild().


Hope I helped!

~TDP

1 comment

0

Tip 2 also means the first waitforchild in a script means you no longer have to use waitforchild on the same instance again

This discussion is preserved as it was. New answers, comments, and votes are closed.