Tool not going in Backpack

Hi, so I am trying to make a Check In, but I fail by giving the player the room card, when I click give it wont give a random card.

local CheckInGui = script.Parent.CheckInGui
local ClickDetector1 = game.Workspace.CheckIn1.Part.ClickDetector
local Players = game:GetService("Players")
local LPlayers = Players.LocalPlayer

function getPlrs(input) 
	local plrs = game.Players:GetPlayers()
	local results = {}

	for i,v in pairs (plrs) do
		if v.Name:sub(1,#input):lower() == input:lower() then
			table.insert(results,v.Name)
	end
end
	if #results == 1 then
		return results[1]
	else
		return {}
	end
	
end

CheckInGui.Frame.PlyrName:GetPropertyChangedSignal("Text"):Connect(function()
	availablePlayers = getPlrs(CheckInGui.Frame.PlyrName.Text)
end)

CheckInGui.Frame.TextButton.MouseButton1Click:Connect(function()
	for i, v in pairs(availablePlayers) do
		local cards = game:GetService("ServerStorage").RoomCards:GetChildren()
		local randomCard = cards[math.random(1,#cards)]:Clone() 
		randomCard.Parent = v.Backpack
	end
end)

ClickDetector1.MouseClick:Connect(function()
	CheckInGui.Frame.Visible = true
end)

Sorry for perhaps an obvious question, but this is a regular script, correct? Not a localscript?
I’m asking because CheckInGui is apparently the parent of this script, and I can only imagine putting localscripts in a GUI.

It is a local script, in StarterGui.

Try to put the tool in Starter Pack with the scripts.

Sorry if I’m way wrong, I’m new to scripting.

If the game has FilteringEnabled, giving a card to a player from a LocalScript will only replicate to that client. You will need to fire a function/event to the server and the server should be in charge of giving the tool.

1 Like

As @Salinas23 said, LocalScripts can’t hand out tools to players. You’ll have to make use of RemoteEvents to give players their card on clicking the buttons.

RemoteEvents receive client-requests such as getting the card. RemoteEvents are controlled via regular scripts (not localscripts), which means the card will be given to the player.

It should be:

v.Name:lower():sub(1, #input:lower()) == input:lower()

Other than that, I see no further errors in your code.

You should also use ipairs since you’re looping over an array, pairs is mainly used for dictionaries and :GetPlayers() returns an array.

You should use a RemoteEvent and a ServerScript to give the card to the player. The card might be given to the player, however it will only happen on the client; not on the server.