local GroupId = 8344746
local Reward = 500
game.Players.PlayerAdded:Connect(function(player)
local stats = player:FindFirstChild("leaderstats")
local Coins = stats:FindFirstChild("Coins")
if player:IsInGroup(GroupId) then
Coins.Value = Coins.Value + Reward
end
end)
What it does is when a player joins my group, and joins the game, they get 500 coins in their leaderstats. I have the DataStore script in a separate script but what I’m trying to do is once the player rejoins the game, they won’t get the “reward” again. Any help?
What you could do, is add a BoolValue that checks if the player has joined the game once or not, if they haven’t then you can set the BoolValue to true & reward the player once
If they’ve played the game more than once, & the BoolValue is already set to true when the DataStore loads, it won’t reward them more than twice
You could implement the BoolValue in the same way you first fire your PlayerAdded event, although you might need to encase both your DataStore script & PlayerAdded script into 1 major script for it to work
The thing is when I add this script into the script in where I have the Datastore in, it doesn’t seem to be working… so I had to add it on a different script
local DataStoreService = game:GetService("DataStoreService")
local DataC = DataStoreService:GetDataStore("DataC")
local GroupId = 8344746
local Reward = 500
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new ("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
--Coin leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
--Reward Part
if player:IsInGroup(GroupId) then
Coins.Value = Coins.Value + Reward
end
--Round leaderstats
local Rounds = Instance.new("IntValue")
Rounds.Name = "Rounds"
Rounds.Value = 0
Rounds.Parent = leaderstats
local playerUserId = "Player_".. player.UserId
print(playerUserId)
local Data
local success, errormessage = pcall(function()
Data = DataC:GetAsync(playerUserId)
end)
if success then
Coins.Value = Data.Coins
Rounds.Value = Data.Rounds
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_".. player.UserId
local Data = {
Coins = player.leaderstats.Coins.Value;
Rounds = player.leaderstats.Rounds.Value;
}
local success, errormessage = pcall(function()
DataC:SetAsync(playerUserId, Data)
end)
if success and Data then
print("Data Succesfully Saved")
else
print("Could not save Data")
warn (errormessage)
end
end)
You would need to use of DataStoreService, here is a sample of code.
local DSS = game:GetService('DataStoreService')
local DataStore = DSS:GetDataStore('DATASTORENAME')
game.Players.PlayerAdded:Connect(function(P)
if not DataStore:GetAsync(P.UserId) then -- lets make sure they didnt already join
-- give your rewards
DataStore:SetAsync(P.UserId, true) -- set the value to true
end
end)
game.Players.PlayerAdded:Connect(function(P)
if DataStore:GetAsync(P.UserId) == true then -- check if the value is true
-- player joined game already
end
end)
local DataStoreService = game:GetService("DataStoreService")
local DataC = DataStoreService:GetDataStore("DataC")
local GroupId = 8344746
local Reward = 500
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new ("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
--Coin leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
--Reward Part
if player:IsInGroup(GroupId) then
Coins.Value = Coins.Value + Reward
end
--Round leaderstats
local Rounds = Instance.new("IntValue")
Rounds.Name = "Rounds"
Rounds.Value = 0
Rounds.Parent = leaderstats
local playerUserId = "Player_".. player.UserId
print(playerUserId)
local Data
local success, errormessage = pcall(function()
Data = DataC:GetAsync(playerUserId)
end)
if success then
Coins.Value = Data.Coins
Rounds.Value = Data.Rounds
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_".. player.UserId
local Data = {
Coins = player.leaderstats.Coins.Value;
Rounds = player.leaderstats.Rounds.Value;
}
local success, errormessage = pcall(function()
DataC:SetAsync(playerUserId, Data)
end)
if success and Data then
print("Data Succesfully Saved")
else
print("Could not save Data")
warn (errormessage)
end
end)
Here it is with the reward script inside the data script.
Add another BoolValue called “Rewarded”, which will be set by default upon creation
Move your “Reward part” after you call your GetAsync & success function
Also check if the BoolValue is equal to false, that way it’ll reward the player once & setting the value to true
It would look something like this overall
local DataStoreService = game:GetService("DataStoreService")
local DataC = DataStoreService:GetDataStore("DataC")
local GroupId = 8344746
local Reward = 500
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new ("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Once = Instance.new("BoolValue")
Once.Name = "RewardOnce"
Once.Parent = player
--Coin leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
--Round leaderstats
local Rounds = Instance.new("IntValue")
Rounds.Name = "Rounds"
Rounds.Value = 0
Rounds.Parent = leaderstats
local playerUserId = "Player_".. player.UserId
print(playerUserId)
local Data
local success, errormessage = pcall(function()
Data = DataC:GetAsync(playerUserId)
end)
if success and Data then
Coins.Value = Data.Coins
Rounds.Value = Data.Rounds
Once.Value = Data.RewardOnce
end
--Reward Part
if player:IsInGroup(GroupId) and Once.Value == false then
Once.Value = true
Coins.Value = Coins.Value + Reward
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_".. player.UserId
local Data = {
Coins = player.leaderstats.Coins.Value;
Rounds = player.leaderstats.Rounds.Value;
RewardOnce = player.RewardOnce.Value;
}
local success, errormessage = pcall(function()
DataC:SetAsync(playerUserId, Data)
end)
if success and Data then
print("Data Succesfully Saved")
else
print("Could not save Data")
warn (errormessage)
end
end)
Inhale
After 50 hours of dumb mistakes I think I have it fixed now, if there’s any more errors or issues I’ll triple-check BUT I AM SURE that I have proof-read everything