Hand To Command (Accept/Decline GUI Help)

Hello! Recently I’ve been making a HandTo Command, and I’ve come up with an issue and question on :

How can I send a ScreenGui to the player, that says “Accept/Decline” and then if they accept the request of someone asking to “give them the tool” then it will put the tool in their backpack.

Now the main importance of this that I’m misunderstanding is how to send the TargetedPlayer, and the TargetedTool to the client side from the server side.

What I have started :

  • Serverscript
--[[

	@since 08/07/2022
	@author TwinPlayzDev
	
	youtube.com/c/TwinPlayz_YT for more information on this video.
	
	This is an updated version of v1. Which didn't include the confirmation GUI.
	
--]]

--[[ SERVICES  ]]--

local Players = game:GetService("Players")						-- GETTING ALL PLAYERS

--[[ VARIABLES ]]--

local Prefix = script:GetAttribute("Prefix")					-- COMMAND PREFIX
local GroupID = 8428801 										-- PUT YOUR GROUP ID HERE
local MinRank = 5 												-- PUT MINIMUM RANK TO USE COMMAND HERE
local GUI = script:WaitForChild("AcceptGUI")					-- GUI WE USE

--[[ FUNCTIONS ]]--

game.Players.PlayerAdded:Connect(function(player) 				-- When they join
	player.CharacterAppearanceLoaded:Connect(function(char) 	-- When their character loads
		player.Chatted:Connect(function(message)				-- When they chat

			if player:IsInGroup(GroupID) then 					-- We check if their in group

				local lower = message:lower() 					-- Getting the message lower
				local args = lower:split(" ")					-- Splitting the message

				--[ UNIFORM COMMAND ]--

				if player:GetRankInGroup(GroupID) >= MinRank then				-- Check if their ranked high enough
					if args[1] == Prefix.."handto" then							-- Check if its command
					
						local function GetPlayer(Input)							-- Local function get player
							for _, Player in ipairs(Players:GetPlayers()) do	-- Checks all players
								if (string.lower(Input) == string.sub(string.lower(Player.Name), 1, #Input)) then
									return Player								-- Returns the player we found
								end
							end
						end
						
						if not args[2] then return end 							-- If they didn't type a players name
						
						local targetPlayer = GetPlayer(args[2])					-- Getting the player
						
						if targetPlayer and targetPlayer:IsA("Player") then		-- If we found the player
							print(targetPlayer.Name)							-- Print their name
							local TargetTool = player.Character:FindFirstChildOfClass("Tool") -- Find Tool to give
							if TargetTool then
								print("giving tool")
								local NewGUI = GUI:Clone()								-- Clones GUI
								NewGUI.Frame.Info = "Player: ".. targetPlayer.Name .. ", would like to send you: "..TargetTool.Name
								NewGUI.Parent = targetPlayer:WaitForChild("PlayerGui")	-- Gives GUI
                                -- WHERE YOU WOULD SEND THE TOOL
							end
						end
						
					end
				else
					print("Not Ranked High Enough")				-- Letting us know their not ranked high enough
				end

			else
				print("Not In Group")							-- Letting us know their not in group
			end
			
		end)
	end)
end)
  • Local Script
--[[

	@since 08/07/2022
	@author TwinPlayzDev
	
	youtube.com/c/TwinPlayz_YT for more information on this video.
	
	This is an updated version of v1. Which didn't include the confirmation GUI.
	
--]]

--[[ VARIABLES ]]--

local AcceptButton = script.Parent:WaitForChild("ACCEPT")
local DeclineButton = script.Parent:WaitForChild("DECLINE")
local GUI = script.Parent.Parent

--[[ FUNCTIONS ]]--

AcceptButton.Activated:Connect(function()
	GUI:Destroy()
end)

DeclineButton.Activated:Connect(function()
	GUI:Destroy()
end)

It’s already made and ready, just curious how I could send the tool,to the client side, and if they accept the request, then they will receive that tool.

Thank you :slight_smile:

It is not recommended, but you can use a RemoteFunction with :InvokeClient() on the ServerScript and wait for the LocalScript to return something to the invoke.

Though you should put the :InvokeClient() in a pcall, if the player leaves the serverscript will throw a error.

This is my example of :InvokeClient()

  • ServerScript
-- You can add a :InvokeClient() somewhere in your script with a RemoteFunction to call the client. It does not have to be this script.
game.Players.PlayerAdded:Connect(function(Player)
	wait(3)
	while wait(1) do
		pcall(function()
			local Invoke = game.ReplicatedStorage.SampleFunction:InvokeClient(Player)
			if Invoke == true then
				print('Player returned true!')
			elseif Invoke == false then
				print('Player reutrned false!')
			end
		end)
	end
end)
  • LocalScript
local lastCall = false
game.ReplicatedStorage:WaitForChild("SampleFunction").OnClientInvoke = function() -- This does not have to be the same remote name. You can change it or do anything with this script
-- You can put your button script here. EX: Make the UI enabled and then add connections to the button and when they click on a button disable the UI and return the answer (true/false)
	lastCall = not lastCall
	return lastCall
end

Hope this helped, happy scripting.

1 Like

Thank you for the help, that was going to be my first attempt. I just wasn’t certain.

How does this look?

  • Server Script
                       if targetPlayer and targetPlayer:IsA("Player") then		-- If we found the player
							print(targetPlayer.Name)							-- Print their name
							local TargetTool = player.Character:FindFirstChildOfClass("Tool") -- Find Tool to give
							
							if TargetTool then
								print("found target tool")
								local NewGUI = GUI:Clone()								-- Clones GUI
								NewGUI.Frame.Info = "Player: ".. targetPlayer.Name .. ", would like to send you: "..TargetTool.Name
								NewGUI.Parent = targetPlayer:WaitForChild("PlayerGui")	-- Gives GUI
								
								while wait(1) do
									pcall(function()
										local Invoke = RemoteFunction:InvokeClient(targetPlayer)
										if Invoke == true then
											print("Player Accepted Request")
											TargetTool.Parent = targetPlayer.Backpack
										elseif Invoke == false then
											print("Player Declined Request")
											return
										end
									end)
								end
							
							end
							
						end
  • Local Script
RemoteFunction.OnClientInvoke = function()
	
	AcceptButton.Activated:Connect(function()
		RemoteFunction:InvokeServer(true)
		GUI:Destroy()
	end)

	DeclineButton.Activated:Connect(function()
		RemoteFunction:InvokeServer(false)
		GUI:Destroy()
	end)
	
	lastCall = not lastCall
	return lastCall
	
end

What were you using lastCall for?

I was using lastcall just for a return for the example.

Here are the fixed versions:

  • LocalScript
RemoteFunction.OnClientInvoke = function()
local AcceptConnection
local DeclineConnection
	AcceptConnection = AcceptButton.Activated:Connect(function()
		GUI:Destroy()
DeclineConnection:Disconnect()
AcceptConnection:Disconnect()
		return true
	end)
	DeclineConnection = DeclineButton.Activated:Connect(function()
GUI:Destroy()
DeclineConnection:Disconnect()
AcceptConnection:Disconnect()
		return false
	end)
end
  • ServerScript
                       if targetPlayer and targetPlayer:IsA("Player") then		-- If we found the player
							print(targetPlayer.Name)							-- Print their name
							local TargetTool = player.Character:FindFirstChildOfClass("Tool") -- Find Tool to give
							
							if TargetTool then
								print("found target tool")
								local NewGUI = GUI:Clone()								-- Clones GUI
								NewGUI.Frame.Info = "Player: ".. targetPlayer.Name .. ", would like to send you: "..TargetTool.Name
								NewGUI.Parent = targetPlayer:WaitForChild("PlayerGui")	-- Gives GUI
								
									pcall(function()
										local Invoke = RemoteFunction:InvokeClient(targetPlayer)
										if Invoke == true then
											print("Player Accepted Request")
											TargetTool.Parent = targetPlayer.Backpack
										elseif Invoke == false then
											print("Player Declined Request")
											return
										end
									end)
							
							end
							
						end

Note: If you saw the unfinished reply It’s because I pressed enter before finishing.

1 Like