Coins increasing by 1 every second. Proximity prompt not working when reaching 15 coins

2 Likes

Because you destroy it when it’s equal to 15?

3 Likes

But ive said when it gets triggered its going to get destroyed

2 Likes


Its letting me do this

youre checking if your coins equal 15 so when you have more it wont work, replace the == with >= to check if the coins are more than or equal to 15

2 Likes

That doesnt seem the case, ive just tried it, unable to work

1 Like

oh yeah just figured out your while true do loop is stopping the proximityprompt code, copy the whole while true do part and the lines below it and paste them under the proximityprompt code




also isnt increasing by 1

this was what you were saying for the script

The other way, paste the Trigged Event in the while true loop

2 Likes

I would handle the while loop in a different script, as you can never escape a while loop. That is, of course, if you don’t use a break. But in your case, a break seems unnecessary.

Edit: You could also use coolyoshi’s method, as that would be a little more efficient.

1 Like
game.Workspace.hi.ProximityPrompt.Triggered:Connect(function(hit)
end)

while true do 
end

like this

2 Likes

like this?

1 Like

if i put it in a different script how would i get that to work

This is how I would do it:

game.Players.PlayerAdded:Connect(function(player)
	while true do
		task.wait(1)
		player.leaderstats.coins.Value += 1
	end
end)

Edit: Again, I highly recommend you use yoshi’s method with task.spawn, as I would say that is more script-efficient

1 Like

Here task.spawn might help you, just replace this with the while true loop and triggered event.


task.spawn(function()
	while true do
		wait(1)
		coins.Value = coins.Value + 1
	end
end)

game.Workspace:WaitForChild("hi").ProximityPrompt.Triggered:Connect(function()
	if coins.Value >= 15  then
		workspace.hi.Transparency = 1
		workspace.hi.ProximityPrompt:Destroy()
	end
end)
1 Like

I didn’t even think of that! I forgot that task.spawn allows code below it to run even while the function is running!

1 Like

Oh my, I just found the problem! YOU HAVE TO SAY game.Workspace NOT JUST workspace

1 Like

You need to put the while true loop under the ProximityPrompt Triggered event.

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 0
	
	game.Workspace.hi.ProximityPrompt.Triggered:Connect(function(hit)
		if coins.Value >= 15 then
			workspace.hi.Transparency = 1
			workspace.hi.ProximityPrompt:Destroy()
		end
	end)

	while true do
		task.wait(1)
		coins.Value += 1
	end
end)
1 Like

Nope! You can totally say workspace instead of game.Workspace. The only difference is organization and how they look. I have linked a post below that explains the difference.

1 Like