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

My for i = 1, Players do is not working, My hand to script wont work! How do I fix this?

I'm making a cafe game, But my Hand To is not working. I have 2 scripts, 1 script and 1 local script.

LOCAL SCRIPT

local Player = game.Players.LocalPlayer
local HandtoGui = script.Parent
local NameBox = HandtoGui.NameBox
local GiveButton = HandtoGui.GiveButton

local groupId = 8513797
local minimumRankToUseGui = 2

if Player:GetRankInGroup(groupId) < minimumRankToUseGui then
	HandtoGui:Destroy()
end

local function getPlayerFromPartialName(PartialName)
	local foundName = nil
	local Players = game.Players:GetPlayers()
	for i = 1, Players do
		local PossiblePlayer = Players[i]
		if string.find(string.lower(PossiblePlayer.Name), string.lower(PartialName)) then
			foundName = PossiblePlayer.Name
		end
	end

	if not foundName then
		return nil
	else
		return foundName
	end
end
GiveButton.MouseButton1Click:Connect(function()
	if not Player.Character:FindFirstChildWhichIsA("Tool") then
		NameBox.Text = ""
		NameBox.PlaceholderText = "Equip a Tool First!"
		wait(1)
		NameBox.PlaceholderText = "Player Name Here"
	end
	local NameBoxText = NameBox.Text
	if NameBoxText ~= "" then
		local playerName = getPlayerFromPartialName(NameBoxText)
		if playerName then
			print("Found player")
			game.ReplicatedStorage.GivePlayerItem:FireServer(playerName)
			NameBox.Text = ""
			NameBox.PlaceholderText = "Gave!"
			wait(1)
			NameBox.PlaceholderText = "Player Name Here"
		else
			NameBox.Text = ""
			NameBox.PlaceholderText = "Player Not Found!"
			wait(1)
			NameBox.PlaceholderText = "Player Name Here"
		end
	end
end)

SCRIPT

game.ReplicatedStorage.Events.GivePlayerItem.OnServerEvent:Connect(function(Player, PlayerName)
	local ToolToGive = Player.Character:FindFirstChildWhichIsA("Tool")
	ToolToGive.Parent = game.Players[PlayerName].Backpack 
end)

OUTPUT

Players.memesfordaysa.PlayerGui.HandtoGui.HandTo:16: invalid 'for' limit (number expected, got table)

It something to do with this bit

for i = 1, Players do

But I don't understand.

1 comment

0

To get the number of items of the table, use #Variable. The hashtag returns the length of something. # <-

1 answer

✓ Accepted answer

Use for i = 1, #Players.

Your error is saying that what i goes up to needs to be a number, and you gave it the array of players. #Players will return the length of the game.Players:GetPlayers() array, which is just how many players are in the array.

2 comments

0

Thanks, but now it gives me this error. GivePlayerItem is not a valid member of ReplicatedStorage "ReplicatedStorage"

0

Never mind i think i fixed it

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