How do I give rewards to player once?

So I got this script:

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

1 Like

How so do I add a BoolValue and where to?

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)

when I add it into that, it doesn’t even work.

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)

Could we see the DataStore script that organizes the data then?


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.

Alright here’s what I’d do:

  • 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)
1 Like

Leaderstats doesn’t seem to show when I add all of those

I think I missed something, could you try again?

Gives an error and leaderstat doesn’t show up

I’m dumb for the 50th time, edited it again :neutral_face:

This is what happens lol
image
I’m confused, do I have to add a BoolValue in ReplicatedStorage or something and name it “Rewarded”

Wait, what the heck? How come it’s set as a Int/NumberValue even though it’s supposed to be a Bool?

No it won’t be replicated to all players that way, you’d need to create the value & load the Check using GetAsync

Change the Once Parent’s to the player I guess?

Alr so the “RewardOnce” leaderstats goes away but doesn’t add 500 coins :sweat_smile:

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

1 Like
local success, errormessage = pcall(function()
	return Data = DataC:GetAsync(playerUserId)
end)

Also then check the output if the data is loaded successfully

if success then
    Coins.Value = Data.Coins
    Rounds.Value = Data.Rounds
    Once.Value = Data.RewardOnce
    print("Successfully loaded player data")
end

lol it works!! Mind telling me what the error was tho? would love to know why it wasn’t working