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:
This is my code:
`game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
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`
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)
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?
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)
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
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.