Script doesn't work (gives script timeout error)

You can write your topic however you want, but you need to answer these questions:
I am trying to make it so that when a player touches a coin, they will get +1 gold.

this script gives me this error:
image

This is my code:

`game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local gold = Instance.new(“IntValue”)
gold.Name = “gold”
gold.Value = 0
gold.Parent = leaderstats
end)

while true do
spawn(function()
while true do
game.Workspace.Coins:WaitForChild(‘Coin’).Ccoin.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local gold = player.leaderstats.gold
gold.Value = gold.Value + 1
hit:Remove()
end
end)
end
end)
wait(0.1)
end`

also sorry for so many edits. I can’t get the code block thing to work.

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new(“Folder”)
    leaderstats.Name = “leaderstats”
    leaderstats.Parent = player

    local gold = Instance.new(“IntValue”)
    gold.Name = “gold”
    gold.Value = 0
    gold.Parent = leaderstats
end)

while true do
    spawn(function()
        while true do
          game.Workspace.Coins:WaitForChild(‘Coin’).Ccoin.Touched:Connect(function(hit)
              local player = game.Players:GetPlayerFromCharacter(hit.Parent)
              if player then
                  local gold = player.leaderstats.gold
                  gold.Value = gold.Value + 1
                  hit:Remove()
              end
          end)
        end
    end)
    wait(0.1)
end

It seems like you are spawning a function, just to make a connection, every .1 seconds which contains a while loop without a wait called in it.

Based on what you have provided, I think the while loops are useless, as you don’t need to make infinite connections to the Touched event.

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new(“Folder”)
    leaderstats.Name = “leaderstats”
    leaderstats.Parent = player

    local gold = Instance.new(“IntValue”)
    gold.Name = “gold”
    gold.Value = 0
    gold.Parent = leaderstats
end)

game.Workspace.Coins:WaitForChild(‘Coin’).Ccoin.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        local gold = player.leaderstats.gold
        gold.Value = gold.Value + 1
        hit:Remove()
    end
end)

Great. I got this code off of the devforum post that I made recently. I will try this. Thank you for replying.

It only gives me the gold for one coin. Mind you there are more than one coin, sorry. I probably didn’t put enough context.

I’m sorry, I didn’t take that into consideration. Do you have a script which spawns the coins? If so could you provide a snippet of the part where you spawn them?

Sure! it’s right here:

local CoinCloned  = game:GetService("ServerStorage").Coin:Clone()

game.Players.PlayerAdded:Connect(function()
	local coins = {}
	for i = 1, 10 do
		local PositionCFrame = CFrame.new(Vector3.new(math.random(8, 89), 8, math.random(-99,0)))
		local coincc = CoinCloned:Clone()

		coincc:PivotTo(PositionCFrame)   
		coincc.Parent = workspace.Coins
		coins[i] = coincc
	end
	while true do
		task.wait(.01)
		for _, coin: Model in ipairs(coins) do
			coin:PivotTo(coin:GetPivot() * CFrame.Angles(0,.1,0))
		end
	end
end)

I do realize there are a lot of problems with this. I am not very good at de-bugging.

Not sure if this is the most efficient way to do it, but I would put the connection in the for loop like this…

for i = 1, 10 do
	local PositionCFrame = CFrame.new(Vector3.new(math.random(8, 89), 8, math.random(-99,0)))
	local coincc = CoinCloned:Clone()

	coincc:PivotTo(PositionCFrame)   
	coincc.Parent = workspace.Coins
	coins[i] = coincc
	local connection

	connection = coincc.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local gold = player.leaderstats.gold
			gold.Value = gold.Value + 1
			coincc:Remove()
			connection:Disconnect()
		end
	end)
end

…so you make a new connection for each new coin.

how would i make the coins rotate?

I guess wrapping while loop (located below the for loop) in a task.spawn() would work?

for i = 1, 10 do
	local PositionCFrame = CFrame.new(Vector3.new(math.random(8, 89), 8, math.random(-99,0)))
	local coincc = CoinCloned:Clone()

	coincc:PivotTo(PositionCFrame)   
	coincc.Parent = workspace.Coins
	coins[i] = coincc
	local connection

	connection = coincc.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			local gold = player.leaderstats.gold
			gold.Value = gold.Value + 1
			coincc:Remove()
			connection:Disconnect()
		end
	end)

	task.spawn(function()
		while coincc do
			task.wait(.01)
			coincc:PivotTo(coincc:GetPivot() * CFrame.Angles(0,.1,0))
		end
	end)
end

EDIT: If you do this, remove the while loop below the for loop.

1 Like

Thank you this worked! :smiley:

1 Like

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