Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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.

0
To get the number of items of the table, use #Variable. The hashtag returns the length of something. # <- ConsteIeo 63 — 3y

1 answer

Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
3 years ago
Edited 3 years ago

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.

0
Thanks, but now it gives me this error. GivePlayerItem is not a valid member of ReplicatedStorage "ReplicatedStorage" memesfordaysa 2 — 3y
0
Never mind i think i fixed it memesfordaysa 2 — 3y
Ad

Answer this question