How To Script a Selection Of Mobile And PC UI

DeviceSelection.rbxl (36.5 KB)

Main Server Script:

local DeviceDictionary = {}


local DeviceGuiRE = Instance.new("RemoteFunction")
DeviceGuiRE.Name = "DeviceGuiRE"
DeviceGuiRE.Parent = game.ReplicatedStorage


local ReturnDictionary = Instance.new("BindableFunction")
ReturnDictionary.OnInvoke = function()
	return DeviceDictionary
end
ReturnDictionary.Name = "GetDictionary"
ReturnDictionary.Parent = script



DeviceGuiRE.OnServerInvoke = function(Player, Var)
	if DeviceDictionary[Player.UserId] ~= nil then
		return DeviceDictionary[Player.UserId]
	end
	if Var == "mobile" then
		DeviceDictionary[Player.UserId] = Var
		print(Player.Name.." has chosen mobile")
		-- Player selected mobile, do something
		return true
	end
	if Var == "pc" then
		DeviceDictionary[Player.UserId] = Var
		print(Player.Name.." has chosen pc")
		-- Player selected pc, do something
		return true
	end
	return false
end


game.Players.PlayerAdded:Connect(function(Player)
	local PlayerGui = Player:WaitForChild("PlayerGui")
	local Connection
	Connection = Player.CharacterAdded:Connect(function()
		if DeviceDictionary[Player.UserId] ~= nil then
			Connection:Disconnect()
			return
		end
		repeat
			wait(0.1)
			script.DeviceGui:Clone().Parent = PlayerGui
		until
			PlayerGui:FindFirstChild("DeviceGui") ~= nil
	end)
end)


game.Players.PlayerRemoving:Connect(function(Player)
	DeviceDictionary[Player.UserId] = nil
end)

Checking for selection from another server script:

local GetDictionary = game.ServerScriptService.DeviceGuiServer:WaitForChild("GetDictionary")

while wait(1) do

print(GetDictionary:Invoke())

end

Checking for selection from client:

local DeviceGuiRE = game.ReplicatedStorage:WaitForChild("DeviceGuiRE")


while wait(1) do
	local result = DeviceGuiRE:InvokeServer()
	if result == "mobile" or result == "pc" then
		print("I have chosen: " .. result)
		break
	else
		print("I have not chosen a device.")
	end
end

script:Destroy()

Sending the selection when the gui button is clicked:

local DeviceGuiRE = game.ReplicatedStorage:WaitForChild("DeviceGuiRE")


script.Parent.Main.BtnMob.MouseButton1Click:Connect(function()
	if DeviceGuiRE:InvokeServer("mobile") == true then
		script.Parent:Destroy()
	end
end)


script.Parent.Main.BtnPc.MouseButton1Click:Connect(function()
	if DeviceGuiRE:InvokeServer("pc") == true then
		script.Parent:Destroy()
	end
end)
1 Like