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)
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)