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

How can I fix this error when trying to give the winner(s)'s reward?

Asked by
D4_rrk 89
4 years ago

So I was making a script where when the timer ends, the winner(s) will get their reward for winning but when It gets to the reward giving bit of code, it gives me an error.

The script:

local ingamePlayersFolder = game.Workspace:WaitForChild("IngamePlayers"):GetChildren()
            local winnersFolder = game.Workspace:WaitForChild("Winners"):GetChildren()

            if #winnersFolder >= 1 or #ingamePlayersFolder >= 1 then
                stats.Value = "Players win!"

                if #winnersFolder >= 1 then
                    for i = 1, #winnersFolder do
                        game.Players[#winnersFolder[1]].leaderstats.Bux.Value = game.Players[#winnersFolder[1]].leaderstats.Bux.Value + winnersReward
                    end
                end

The error:

Workspace.GameController:79: attempt to get length of a Instance value

0
which line is 79 lmao and show us the line of the instance raid6n 2196 — 4y
0
Line 79 was the line where game.Players[#winnersFolder[1]].leaderstats.Bux.Value = game.Players[#winnersFolder[1]].leaderstats.Bux.Value + winnersReward is which is the same line where the error occurred. D4_rrk 89 — 4y
0
There isn't an Instance but I still get the error which directs me to that. D4_rrk 89 — 4y

1 answer

Log in to vote
1
Answered by
D4_rrk 89
4 years ago

I found out that I could just get the player from the character through :GetPlayerFromCharacter which solves my problem.

local ingamePlayersFolder = game.Workspace:WaitForChild("IngamePlayers"):GetChildren()
            local winnersFolder = game.Workspace:WaitForChild("Winners"):GetChildren()

            if #winnersFolder >= 1 or #ingamePlayersFolder >= 1 then
                stats.Value = "Players win!"

                if #winnersFolder >= 1 then
                    for i = 1, #winnersFolder do
                        local winnerPlr = game.Players:GetPlayerFromCharacter(winnersFolder[1])

                        winnerPlr.leaderstats.Bux.Value = winnerPlr.leaderstats.Bux.Value + winnersReward

                        print("success_winner")
                                               end
                    end
                end
Ad

Answer this question