Hello, I want to create a onetime coin you can only collect it only one time
can I do it without the usage of datastores?.
Any Help is appreciated
I think datastores will be the only way for one time things (in your case a coin).
1 Like
Ok, thanks I already did it but I thought there was another way
2 Likes
You can make a coin which can only be collected once per session.
Otherwise you’d need to make use of datastores.
local players = game:GetService("Players")
local coin = script.Parent
local awardedPlayers = {}
coin.Touched:Connect(function(hit)
local hitModel = hit:FindFirstAncestorOfClass("Model")
if hitModel then
local hitPlayer = players:GetPlayerFromCharacter(hitModel)
if hitPlayer then
if awardedPlayers[hitPlayer] then
return --Already awarded.
end
hitPlayer.leaderstats.Coins.Value += 1000 --Example.
awardedPlayers[hitPlayer] = true --Add to table of awarded players.
end
end
end)
players.PlayerRemoving:Connect(function(player)
awardedPlayers[player] = nil --Remove from table to save memory.
end)
Here’s a primitive approach to a once per session method.
3 Likes
Thanks for the both of you, Both are helpful answers, Sadly i can’t Mark these two as an Solution .
you dont need a datastore to do this lol, you just simply destroy it on touch and change the value
I think by one-time they mean once per player not once per server.
ohhhhhhhhhhhhhhhhhhhhhhhhhhh okay
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")
local coin = script.Parent
local awardedPlayers = {}
coin.Touched:Connect(function(hit)
local hitModel = hit:FindFirstAncestorOfClass("Model")
if hitModel then
local hitPlayer = players:GetPlayerFromCharacter(hitModel)
if hitPlayer then
if awardedPlayers[hitPlayer] then
return --Already awarded.
end
local success1, result1 = pcall(function()
return datastore:GetAsync(hitPlayer.UserId) --Load data from datastore.
end)
if success1 then
if result1 then --Already awarded.
return
else --Not awarded.
local success2, result2 = pcall(function()
datastore:SetAsync(hitPlayer.UserId, true) --Save data to datastore.
end)
end
end
hitPlayer.leaderstats.Coins.Value += 1000 --Example.
awardedPlayers[hitPlayer] = true --Add to table of awarded players.
end
end
end)
players.PlayerRemoving:Connect(function(player)
awardedPlayers[player] = nil --Remove from table to save memory.
end)
I rewrote my previous example and implemented the use of a DataStore.
3 Likes