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

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

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

3 comments

0

which line is 79 lmao and show us the line of the instance

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.

0

There isn't an Instance but I still get the error which directs me to that.

1 answer

✓ Accepted answer

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

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