**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
**What is the issue? it works PERFECTLY in the test environment, but does not work when I publish the game
What solutions have you tried so far? I’ve tried many variations of code
currency = “Money”
amnt = math.random(1,3)
local debounce = false
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”
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
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)
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