I'm getting 2 errors

These are the errors

  • leaderstats is not a valid member of Player “Players.lillsbit” - Server - MainServer:94

  • Infinite yield possible on ‘Players.lillsbit:WaitForChild(“leaderstats”)’ - Studio

MainServer script

1 Like
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TilesFolder = ReplicatedStorage:WaitForChild("Tiles")
local ActiveTilesFolder = workspace:WaitForChild("ActiveTiles")
local tileOffsetHeight = Vector3.new(0,5,0)

local cutFunction = ReplicatedStorage:WaitForChild("Cut")

local playerTiles = {}
local moneyPerTick = 1

local function createLeaderstats(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("NumberValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
	Money.Value = 0
end

local function spawnTile(tileNumber)
	local tile = TilesFolder:FindFirstChild(tostring(tileNumber))
	if tile then
		local newTile = tile:Clone()
		newTile.Parent = ActiveTilesFolder
		return newTile
	end
end

local function removeLastTile()
	local lastTile = ActiveTilesFolder:FindFirstChild(tostring(#Players:GetPlayers() + 1))
	if lastTile then
		lastTile:Destroy()
	end
end

local function moveToTile(player: Player, tile)
	local character = player.Character
	if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") then
		local humanoid = character.Humanoid
		local rootPart = character.HumanoidRootPart
		
		humanoid:MoveTo(tile.Position + tileOffsetHeight)
		humanoid.MoveToFinished:Wait()
		
		rootPart.CFrame = tile.CFrame + Vector3.new(0,2,0)
		rootPart.CFrame = CFrame.new(rootPart.Position, rootPart.Position + tile.CFrame.LookVector)
	end
end

local function unlockPlayerMovement(player: Player)
	local character = player.Character
	if character and character:FindFirstChild("CanMove") then
		character.CanMove.Value = true
		character.Humanoid.WalkSpeed = 16
	end
end

local function lockPlayerMovement(player: Player)
	local character = player.Character 
	if character and character:FindFirstChild("CanMove") then
		character.CanMove.Value = false
		character.Humanoid.WalkSpeed = 16
	end
end

local function moveAllUp()
	for _, player in ipairs(Players:GetPlayers()) do
		local currentTile = playerTiles[player.UserId]
		if currentTile and tonumber(currentTile.Name) > 1 then
			local newTileNumber = tonumber(currentTile.Name) - 1
			local newTile = ActiveTilesFolder:FindFirstChild(tostring(newTileNumber))

			if newTile then
				playerTiles[player.UserId] = newTile
				moveToTile(player, newTile)
			end
		end
	end
end

local function giveMoney()
	while true do
		for _, player in pairs(Players:GetPlayers()) do
			local currentTile = playerTiles[player.UserId]
			if currentTile then
				local distanceFromFront = tonumber(currentTile.Name)
				if distanceFromFront then
					local moneyToAdd = moneyPerTick * math.exp(distanceFromFront ^ (1.1 * #Players:GetPlayers()))
					player:SetAttribute("PerSecond", moneyToAdd)
					player.leaderstats.Money.Value += moneyToAdd
				end
			end
		end
		wait(0.5)
	end
end

spawn(giveMoney)

local function cut(player, cutCost)
	local currentTile = playerTiles[player.UserId]
	local frontTileNumber = tonumber(currentTile.Name) - 1
	local frontTile = ActiveTilesFolder:FindFirstChild(tostring(frontTileNumber))
	
	if frontTile then
		print(currentTile, frontTileNumber, true)
		local frontPlayer = nil
		
		for _, otherPlayer in pairs(Players:GetPlayers()) do
			if playerTiles[otherPlayer.UserId] == frontTile then
				frontPlayer = otherPlayer
				break
			end
		end
		
		if frontPlayer then
			if player.leaderstats.Money.Value >= cutCost then
				playerTiles[player.UserId] = frontTile
				playerTiles[frontPlayer.UserId] = currentTile
				
				task.delay(0, function()
					moveToTile(player, frontTile)
				end)
				task.delay(0, function()
					moveToTile(frontPlayer, currentTile)
				end)
				
				player.leaderstats.Money.Value -= cutCost
				return true
			else
				if player.leaderstats.Money.Value < cutCost then
					warn(player.Name .. " does not have enough money to cut")
				end
			end
		end
	else
		print(currentTile, frontTileNumber, false)
	end
	
	return false
end

cutFunction.OnServerInvoke = function(player, cutCost)
	return cut(player, cutCost)
end

Players.PlayerAdded:Connect(function(player)
	createLeaderstats()
	
	player.CharacterAdded:Connect(function(character)
		local canMove = Instance.new("BoolValue")
		canMove.Name = "CanMove"
		canMove.Value = false
		canMove.Parent = character
		
		lockPlayerMovement(player)
		
		local tileNumber = 1
		while ActiveTilesFolder:FindFirstChild(tostring(tileNumber)) do
			tileNumber += 1
		end

		local tile = spawnTile(tileNumber)
		
		if tile then
			playerTiles[player.UserId] = tile
			local rootPart = character:WaitForChild("HumanoidRootPart")
			rootPart.CFrame = tile.CFrame + tileOffsetHeight
			rootPart.CFrame = CFrame.new(rootPart.Position, rootPart.Position + tile.CFrame.LookVector)
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
    local tile = playerTiles[player.UserId]
	if tile then
		playerTiles[player.UserId] = nil
	end
	
	moveAllUp()
	
	removeLastTile()
end)

in the line where u call the createleaderstats function, do createLeaderstats(player)

1 Like

Could you please send the line where the error is happeing?