Group rewards script

i trying to make a reward for joinig in group, but if any player joined, they dont get and reward (the output says that the players have already joined into the game once), i changed the datastore key much times, but that doesn’t work either.

my script:

local groupId = 35020162
local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	if player:IsInGroup(groupId) then
		print("player " .. player.Name .. " in group")

		local newKey = "PlayerJoined_" .. player.UserId

		local hasJoinedBefore = dataStore:GetAsync(newKey)

		if not hasJoinedBefore then
			dataStore:SetAsync(newKey, true)
			print("player " .. player.Name .. " has joined")
			local remoteEvent = game.ReplicatedStorage:WaitForChild("ShowGUI")
			remoteEvent:FireClient(player)
		end
	end
end)

game.ReplicatedStorage.GetRewards.OnServerEvent:Connect(function(player)
	player.CoinsStats.Coins.Value += 5 
end)

Thats because you should write datastore:SetAsync() in the GetRewards remote event since the player may of not clicked on the button from ShowGui to get the reward yet.And change the datastore key one more time.

1 Like

GetRewards is done by pressing a UIbutton, and even when the key name is changed, nothing changes.

did you change your server script first

code shoul be written like this:

local groupId = 35020162
local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	if player:IsInGroup(groupId) then
		print("player " .. player.Name .. " in group")

		local newKey = "PlayerGroup_" .. player.UserId

		local hasJoinedBefore = dataStore:GetAsync(newKey)

		if not hasJoinedBefore then
			print("player " .. player.Name .. " has joined")
			local remoteEvent = game.ReplicatedStorage:WaitForChild("ShowGUI")
			remoteEvent:FireClient(player)
		end
	end
end)

game.ReplicatedStorage.GetRewards.OnServerEvent:Connect(function(player)
    local newKey = "PlayerGroup_" .. player.UserId
	player.CoinsStats.Coins.Value += 5 
    local success,errormessage=pcall(function()
       dataStore:SetAsync(newKey, true)
    end)
    if not success then
       warn(errormessage)
    end
end)

[/quote]

thanks, it’s working! i’ve been working on this script for 5 hours, I couldn’t figure out what was wrong with it…

cool no problem but in future use Remove:Async() to remove old data in the datastore

1 Like