Make the time for something to respawn save after the player has left

Hello. I have added a coin system in my game, where if you touch a coin, it’s added to your leaderstats. The only problem is that once you rejoin the game, the coins instantly respawn, making it easy to grind coins in the game. Is it possible to make the timer continue after the player has left?

waitTime = 600 -- the time you need to wait for the coin to respawn
amount = 1
function onTouched(part)
	local h = part.Parent:FindFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:FindFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:FindFirstChild("leaderstats")
			if (stats~=nil) then
				local score = stats:FindFirstChild("Coins")
				if (score~=nil) then
					score.Value = score.Value + amount
				end
			end
		end
		script.Parent.Transparency = 1
		script.Disabled = true
		wait(waitTime)
		script.Parent.Transparency = 0
		script.Disabled = false
	end
end

script.Parent.Touched:Connect(onTouched)

Thank you.

Save it with dataStoreService when he leaves and make the wait time 600 or whatever was saved

1 Like

Although I know that that is correct, I don’t really know how I’m supposed to put that in.

Place this before your touched function

local dataStoreService = game:GetService("DataStoreService")
local timeLeft = dataStoreService:GetDatastore("timeLeft")

game.Players.PlayerAdded:Connect(function(player)
	waitTime = timeLeft:GetAsync(player.UserId) or 600
end)

game.Players.PlayerRemoving:Connect(function(player)
	timeLeft:SetAsync(player.UserId, waitTime) 
end)
1 Like

I tried doing that, but it didn’t seem to work. At first, I got an error saying “GetDatastore is not a valid member of DataStoreService “DataStoreService””. However, I fixed that by changing the capitalization from “GetDatastore” to “GetDataStore”.

However, it still doesn’t seem to save. Is there anything I’ve done incorrectly?

waitTime = 600
amount = 1

local dataStoreService = game:GetService("DataStoreService")
local timeLeft = dataStoreService:GetDataStore("timeLeft")

game.Players.PlayerAdded:Connect(function(player)
	waitTime = timeLeft:GetAsync(player.UserId) or 600
end)

game.Players.PlayerRemoving:Connect(function(player)
	timeLeft:SetAsync(player.UserId, waitTime) 
end)

function onTouched(part)
	local h = part.Parent:FindFirstChild("Humanoid")
	if (h~=nil) then
		local thisplr = game.Players:FindFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:FindFirstChild("leaderstats")
			if (stats~=nil) then
				local score = stats:FindFirstChild("Coins")
				if (score~=nil) then
					score.Value = score.Value + amount
				end
			end
		end
		script.Parent.Transparency = 1
		script.Disabled = true
		wait(waitTime)
		
		script.Parent.Transparency = 0
		script.Disabled = false
	end
end

script.Parent.Touched:Connect(onTouched)

I normally use Datastore2 so Im not sure if I wrote them correctly, check that with any of your other dataStore s scripts

1 Like