Infinite Yield Possible on currency, help!

  1. **What do you want to achieve? I put this code into a Part so that when a player touches it, I want (1) their Money value to increase in the leaderstats (2) the coin to randomly appear as guided

  2. **What is the issue? it works PERFECTLY in the test environment, but does not work when I publish the game

  3. What solutions have you tried so far? I’ve tried many variations of code

currency = “Money”
amnt = math.random(1,3)
local debounce = false

position1 = math.random(397,799)
position2 = math.random(159,159)
position3 = math.random(-11,400)

if amnt == 1 then
script.Parent.MoneyGui.Gui.Text = “+1”
elseif amnt == 2 then
script.Parent.MoneyGui.Gui.Text = “+2”
elseif amnt == 3 then
script.Parent.MoneyGui.Gui.Text = “+3”
end

local function onTouch(Part)

if debounce then
	return
end

local ThisPlayer = game.Players:findFirstChild(Part.Parent.Name)
ThisPlayer.leaderstats:findFirstChild(currency).Value = ThisPlayer.leaderstats:findFirstChild(currency).Value + amnt
debounce = true

script.Parent.Position = Vector3.new(-73, 167, 135)

wait(4)

script.Disabled = true
wait(0.1)
script.Disabled = false
script.Parent.Position = Vector3.new(position1,position2,position3)
debounce = false

end

script.Parent.Touched:Connect(onTouch)

— on my ServerScriptService:

function onPlayerEntered(newPlayer)
wait(.5)
local stats = Instance.new(“IntValue”)
stats.Name = “leaderstats”

local score = Instance.new("IntValue")
score.Name = "Money"
score.Value = 0
score.Parent = stats
stats.Parent = newPlayer

end
game.Players.ChildAdded:connect(onPlayerEntered)

local Brick = game.Workspace.Part
Brick.Touched:Connect(function(hit)  
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)  
end)

I get this error in the published environment:

I think you left out the part where it waits for “money” (since I don’t see a WaitForChild in your code)

infinite yield usually happens when the script tries to wait for an something that doesn’t exist, or doesn’t yet exist. So in your case, the function is trying to wait for “money” inside of leaderstats.

what I usually do is

local part = workspace:WaitForChild("Part",5) --wait for maximum of 5 seconds

if part then
	--do something
end
1 Like

I updated the line to local Brick = game.Workspace:WaitForChild(“Part”,5) as suggested.

This fixed the infinite yield problem, but now I’m getting:
Workspace.Part.Money:40: attempt to index nil with ‘Value’

on this line:
ThisPlayer.leaderstats:findFirstChild(“currency”).Value = ThisPlayer.leaderstats:findFirstChild(“currency”).Value + amnt

I must be doing something wrong

It should be FindFirstChild, also um game.Players:findFirstChild(Part.Parent.Name) only gets the StringValue, not the actual Player Object

Your code confuses me a bit, but you should also define the Player in your local function onTouch as well

ahh thanks for catching that… it’s weird how that didn’t affect anything in the test mode

Np, honestly what I would do though is check to make sure that a Player is valid as well since

Is attempting to get the Value of the currency, but instead ends up with nil, or something that doesn’t exist (From the script’s standpoint)

local function onTouched(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Player then 
        local Leaderstats = Player:WaitForChild("leaderstats")
        Leaderstats.currency.Value += amnt
    end
end)
2 Likes

SOLVED!
I took your example and needed to change up the leaderstat part in order for it to work:

if ThisPlayer then 
	local Leaderstats = ThisPlayer:WaitForChild("leaderstats")
	local Cash = Instance.new("IntValue", Leaderstats)
	Cash.Name = "Cash"

	Leaderstats.Cash.Value = Leaderstats.Cash.Value + amnt

thanks a bunch!!!

2 Likes