Why does this bug keep happening?

This bug keeps affecting my game, there is a leaderstats but it doesn’t want to be recognised?

image
image

also multiple coins spawn on-top of each other…

local Coin = {}

local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")
local MarketplaceService = game:GetService("MarketplaceService")

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local Tokens = workspace:FindFirstChild("Tokens")
local CoinFolder = ServerStorage:FindFirstChild("Coins")

local CollectionService = game:GetService("CollectionService")

local LS = nil

Coin.CoinTaken = function(player, CoinType, TheCoin, state)

	if not state then
		state = true

		local Amount = 0

		local PlayerToFind = Players:GetPlayerFromCharacter(player.Parent)

		if not PlayerToFind then return end

		CollectionService:GetTagged(TheCoin.Name)

		LS = PlayerToFind:FindFirstChild("leaderstats")

		if TheCoin then
			if MarketplaceService:UserOwnsGamePassAsync(PlayerToFind.UserId, 147063459) then
				if TheCoin:FindFirstChild("Amount") then
					LS.Tokens.Value += TheCoin.Amount.Value * 2
				end
			else
				LS.Tokens.Value += TheCoin.Amount.Value
			end
		end
		
		Amount = 5
		
		if TheCoin:FindFirstChild("Timer") then
			Amount = TheCoin.Timer.Value
		end

		local PreviousPos = TheCoin.Position
		TheCoin:Destroy()

		-- WAIT 5 seconds to spawn that coin again
		task.delay(Amount, function()
			Coin.New(PreviousPos, CoinType)
			state = true
		end)
	end
end

Coin.New = function(position, Type)
	local desiredCoin = CoinFolder:FindFirstChild(Type)
	local NewCoin = desiredCoin:Clone()

	CollectionService:AddTag(NewCoin, "Coin")

	NewCoin.Parent = Tokens
	NewCoin.Position = position

	NewCoin.Touched:Connect(function(player)
		Coin.CoinTaken(player, Type, NewCoin, false)
	end)
end

return Coin

Seems like some of your coins, specifically "Token"s do not have an amount value, your script will check for an Amount value only if they have the gamepass, you should add a similar check around both branches like so

if TheCoin and TheCoin:FindFirstChild("Amount") then
	if MarketplaceService:UserOwnsGamePassAsync(PlayerToFind.UserId, 147063459) then
		LS.Tokens.Value += TheCoin.Amount.Value * 2
	else
		LS.Tokens.Value += TheCoin.Amount.Value
	end
end

For the first error we would have to see your leadstats code, I suspect it is just slow to create the leaderstats and if the player picks up a token before it is finished you get this error.

I think the coin may be deleted too fast which causes the error. As it checks for the amount which has just been deleted alongside the coin

I don’t think so, the Destroy call happens at the end of your function. The coin will be valid all the way through and naturally debounces since destruction removes connections.

I also notice you run :GetTagged but don’t do anything with the results, worth it to just remove that line.

The main leaderstats code is here, the token script is a seperate one:

game.Players.PlayerAdded:Connect(function(plr)
	-- Setup Data (Display)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local stage = Instance.new("IntValue", leaderstats)
	stage.Name = "Stage"
	
	local questsFolder = Instance.new("Folder")
	questsFolder.Name = "Quests"
	questsFolder.Parent = plr
	
	local Rebirths = Instance.new("IntValue", leaderstats)
	Rebirths.Name = "Rebirths"
	
	local dataFolder = Instance.new("Folder", plr)
	dataFolder.Name = "Data"
	
	local itemsFolder = Instance.new("Folder", dataFolder)
	itemsFolder.Name = "Items"
	
	local Tstage = Instance.new("IntValue", plr)
	Tstage.Name = "TStage" 
	
	local function produceInstance(name, typeOfInstance : string, location, defaultValue)
		local making = Instance.new(typeOfInstance, location)
		making.Name = name
		making.Value = defaultValue
		return making
	end

	local FreeSkipTimeMin = produceInstance("FreeSkipTimeMin", "IntValue", dataFolder, 39)
	local FreeSkips = produceInstance("FreeSkips", "IntValue", dataFolder, 0)
	
	local key = "ID_".. tostring(plr.UserId)
	
	local data
	local success, error = pcall(function()
		data = DS:GetAsync(key)
	end)
	
	-- Load Data
	if success and data then
		stage.Value = data.stage
		Rebirths.Value = data.Rebirths
		
		FreeSkips.Value = data.free_skips.amount
		FreeSkipTimeMin.Value = data.free_skips.mins
		Tstage.Value = stage.Value -- (TStage stands for the Stage the player is at)
		print("Data loaded: ".. key.. " (".. plr.Name.. "'s data: ".. tostring(stage.Value).. ")")
	else
		warn("Data ".. key.. " Unsuccessful when loading with error \"".. error.. "\"")
	end
end)

Seems like you do not have a Tokens leaderstat.

I do, it’s just that its a seperate script mentioned before:

local MarketplaceService = game:GetService("MarketplaceService")

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local BossRE = Remotes:WaitForChild("BossInfo")
local RewardRE = Remotes:WaitForChild("RewardRE")

task.wait()

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("TokenSS")

local Stat = "Tokens"
local startNum = 0

Players.PlayerAdded:Connect(function(player)	
	local leaderstats = player:WaitForChild("leaderstats")
	local Tokens = Instance.new("IntValue",leaderstats)
	Tokens.Name = Stat
	Tokens.Value = startNum
	DS:GetAsync(Tokens.Value, player.UserId)
end)

Players.PlayerRemoving:Connect(function(player)
	local PlayerLS = player:WaitForChild("leaderstats")
	local Tokens = PlayerLS:WaitForChild("Tokens")
	DS:SetAsync(player.UserId, Tokens.Value)
end)

local function RewardToken(player, TokenReward)
	local PlayerLS = player:WaitForChild("leaderstats")
	local Tokens = PlayerLS:WaitForChild("Tokens")

	Tokens.Value += TokenReward

	if MarketplaceService:UserOwnsGamePassAsync(player.UserId, 147063459) then
		Tokens.Value += TokenReward * 2
	else
		Tokens.Value += TokenReward
	end
end
	
BossRE.OnServerEvent:Connect(function(Player, TokenReward)
	RewardToken(Player, TokenReward)
end)

RewardRE.OnServerEvent:Connect(function(player, TokenReward)
	RewardToken(Player, TokenReward)
end)

You do have a random task.wait() at the start of that script, it’s not much but if you are the first player to join it may not connect PlayerAdded in time. Avoid yields before connections as much as possible.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.