How do I give an item to player through GUI?

  1. What do you want to achieve?
    I’ve created a GUI where I type in a player’s username and item name (since I’m doing it for a hotel, I would type in the room’s number)
  2. What is the issue?
    When I type the username and room number in the TextBox and then press ‘Confirm’, it doesn’t give the item.
  3. What solutions have you tried so far?
    I’ve tried including values. I tried this for example room number’s value:
script.Parent.Value = script.Parent.Parent.roomno.Text

Script is inside the value, and ‘roomno’ is a TextBox inside a ScreenGui. Text should be the text inside TextBox.
I’ve made the giving script using variables and I’m pretty sure I’ve done several things wrong.

local roomNo = script.Parent.Parent.Room.Value --Value should be room # from TextBox
local user = script.Parent.Parent.Username.Value --Value should be username from TextBox

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Parent.Enabled = false --Closes the Check In GUI
local tool = game.Lighting:FindFirstChild(roomNo) --Room cards from Lighting (tools: 101, 102 etc.)
local klone = tool:Clone()
	if klone.Parent ~= user.Backpack then
		klone.Parent = user.Backpack
	end
end)

Gives error when clicking on confirm: Players.Sander_rr.PlayerGui.CheckIn.Frame.confirm.LocalScript:7: attempt to index nil with ‘Clone’

I’m a beginner to scripting and I only know very basic stuff. It could be too hard thing for me to make too, because I only know very little of scripting. Please no judging if the script is really dumb.

Oh, and also, another problem - I want the GUI to open when I click on a part. For some reason, it doesn’t open. Script inside ClickDetector:

script.Parent.MouseClick:Connect(function()
	game.StarterGui.CheckIn.Enabled = true
end)
2 Likes

You would want ot use a RemoteEvent to clone the tool on the server and give it to the player
You can check out remote events and their uses on the API here:
https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

In essense however you would want to use something like this:

--local script
RemoteEvent:FireServer()

--server script
local tool = --tool here
RemoteEvent.OnClientEvent:Connect(function(plr)
tool:Clone().Parent = plr.Backpack
end)
2 Likes

that is because you are doing this server side, in order to modify UI elemtents client sided then you will need remote events. If you dont know what client and server side are then you should read up on these articles to get to know them https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

if this helpls dont forget to mark this as a solution :grin:

Thanks. I unfortunately forgot to mention that the text in TextBox doesn’t even go to the Values, so I couldn’t use the values to give items. And I’m still bit confused about all this stuff.
Inside the TextButton (confirm) do I type:

script.Parent.MouseButton1Click:Connect(function()
	workspace.GiveRoom:FireServer()
end)

?
And then inside the script inside the RemoteEvent (GiveRoom):

script.Parent.OnClientEvent:Connect(function()
	
local roomNo = script.Parent.Parent.Room.Value --Value should be room # from TextBox
local user = script.Parent.Parent.Username.Value --Value should be username from TextBox

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Parent.Enabled = false --Closes the Check In GUI
	local tool = game.Lighting:FindFirstChild(roomNo) --Room cards from Lighting (tools: 101, 102 etc.)
local klone = tool:Clone()
	if klone.Parent ~= user.Backpack then
		klone.Parent = user.Backpack
	end
end)
end)

?
Am I doing this right or have I made it even worse now?
And yeah, there’s still issues with values. Values are empty all the time and Text from TextBox just aren’t as values.

define the values inside the MouseButton1Click event since they will change (i assume so)

So when someone clicks the textbox you want to fire the server with the remote event. You may also wish to send across info about what tool it is for different buttons which could look like this:

--local script
script.Parent.MouseButton1Click:Connect(function() -- button on UI which is being pressed
RemoteEvent:FireServer('Sword')-- firing server with the tool I want given
end)

-- server script

RemoteEvent:OnServerEvent:Connect(function(plr,Tool) -- by default first value will be player who fired the event, what follows is whatever values you send from the client, in this case 'Sword;
if Tool == 'Sword' -- you can do elseifs to go though different tools depending on what values are sent by the client
game.ServerStorage:FindFirstChild(Tool):Clone().Parent = plr.Backpack --now going though ServerStorage to find the tool I want (in this case Sword)
end
end)

I don’t see the purpose of that, why does he need a RemoteEvent when he can do it all on client?
Besides, an exploiter can easily fire that remote event too so it won’t really affect security.

You cannot clone a tool on a client. He needs to handle the tool giving on the server otherwise it wont work

I think it all would be working if the text inside TextBox actually would go into Text property…

I changed my scripts a little and used some parts of your code (thanks!)
I still get error though
Workspace.GiveRoom.Script:7: attempt to index nil with ‘Clone’
Source leads to this line:
game.ServerStorage:FindFirstChild(Tool):Clone().Parent = user.Backpack

That means it is not finding the tool that you have refrenced

It doesn’t find it because what I type into TextBox doesn’t go into ‘Text’. I have set the tool as the text in TextBox, but if I type, for example, 102 in the textbox, it doesn’t go to the Text property, it just stays empty. That’s another issue I have to fix. Same issue with the username.

So ik this was a year ago but I think I see your problem @Sander_rr

so in your script to get the text out of a textbook your doing .value but isn’t a value only for a value shouldn’t it be more like this:

local roomNo = script.Parent.Parent.Room.Text --Value should be room # from TextBox
local user = script.Parent.Parent.Username.Text --Value should be username from TextBox

can’t you just like…clone it from the client side ;-;