One-time event on join

How do I get something to happen only once when the player joins a server, like they join a group and join the game it gives them X amount of coins but only once.

I know it’s something regarding DataStores, but I’m not sure on how to construct it.

2 Likes

You can use a BoolValue and store them inside a player’s folder.

How do I tell if they’ve joined after joining the group though

Use
If plr:IsInGroup(groupid) then

So I figured it out.
I create a BoolValue FirstJoin when they join, clone it to their folder and set it to True, and adds their coins. When they join again, it sets to False and doesn’t add their coins again.

5 Likes

How’s this for a start?

wait (1)
local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local Bool = Instance.new("Folder")
	Bool.Parent = player
	
	local FirstJoin = Instance.new("BoolValue")
	FirstJoin.Parent = Bool
	FirstJoin.Value = true

end)

Looks like it’s fine. Did you want to make it so that boolvalue only adds to the player if the player is in a certain group?

Yeah, I’ve just gone through and thought something. Won’t the Bool folder be created every time the player joins?
Also, I’ll need some more help constructing this.

GroupId = 5901185
local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local Bool = Instance.new("Folder")
	Bool.Parent = player
	
	local FirstJoin = Instance.new("BoolValue")
	FirstJoin.Parent = Bool
	FirstJoin.Value = true
	
	if FirstJoin and player:IsInGroup(GroupId) then
		local Coins = player:WaitForChild("leaderstats").Coins
		Coins.Value = Coins.Value + 1500
	end
end)

Answer to your question is yeah, I need the coins to be added if they are in a certain group

Yeah, looks good.

You are correct however it will run each time a player joins the game. If you only want it to happen once for each player no matter how many times they re-join you will probably need to look into using a datastore to save information saying the player had joined before.

How can I declare if the player has joined before?

You could try adding a boolValue that saves when a player leaves the game. Then you can make it so that if the boolValue is false when the player joins, they don’t get any money.

Quiack question regarding DS access, where can I get these DataStore ‘codes’ that are implemented after GetDataStore:

local SaveDataStore = DataStoreService:GetDataStore("ObbyBlitzDataStore-U3D8C3FYS")

Or are they randomly created by the developer

So those are the Datastore Keys. They’re created by the developer and can be pretty much whatever you like. For players it would be a good idea to use the player UserID since they can change their name.

You can create if yourself, so you can make it whatever you want.

I’ve been experimenting with DS around recently and this might be pretty useful if you want to check if the player is new to the game or not.

local DataStore = game:GetService("DataStoreService")
local FirstTime = DataStore:GetDataStore("CheckIfJoeineedBefore") -- Change to any name you want.

game.Players.PlayerAdded:connect(function(Player)
	if not FirstTime:GetAsync(Player.UserId) then
		print("NewPlayer")
	else
		print("NotNewPlayer")
	end
	FirstTime:SetAsync(Player.UserId, "JoinedBefore")
end)

Where in that code does it recognise if the player is new or not?

Okay so it prints NewPlayer and NotNewPlayer correctly, but now I’ve got another error.

If a player has joined the game before, then joins the group and rejoins the game, it won’t work because the player is not a new player to the game.

1 Like

Hmm… you want the check if the player is new and also in the group? and If so it’ll record the player as “not-so-new” anymore right? in that case…

local DataStore = game:GetService("DataStoreService")
local FirstTime = DataStore:GetDataStore("CheckIfJoeineedBefore") -- Change to any name you want.

game.Players.PlayerAdded:connect(function(Player)
	if not FirstTime:GetAsync(Player.UserId) then -- Check if the player join the game for the first time
		if Player:IsInGroup(123456) then -- Check if the player is in the group
			FirstTime:SetAsync(Player.UserId, "JoinedBefore") -- Save the data that player has joined the game before and is in the group.
			print("NewPlayer that is in the group")
		else
			print("NewPlayer but isn't in the group")
		end
	else
		print("NotNewPlayer / NewPlayer but isn't in the group")
	end
end)

Apology if my explanation within the script is a bit weird. My English isn’t that good.