How to make when rejoin something happens

Hello!

I want to know how do i make that when the player rejoin something changes in the game that the player didnt see before.

It is just like the game “get a snack at 4 pm”

Any help is allowed, Thanks!

3 Likes

I need some solutions. Please help me!

when player joins save it in a data store and then check if player join when he/she joins again

I have tried so many times, Basically i did this code:

local datastore = game:GetService("DataStoreService"):GetDataStore("GameChanging")

game.Players.PlayerAdded:Connect(function(player)
	local Part1 = Instance.new("BoolValue")
	Part1.Parent = player
	Part1.Name = "Part1"
	
	local key = "user-" .. player.userId

	local storeditems = datastore:GetAsync(key)
	if storeditems then
		Part1.Value = storeditems[1]
	else
		local items = {Part1.Value}
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.Part1.Value}
	local key = "user-" .. player.userId

	datastore:SetAsync(key, items)
end)

And another part of code for applying if it’s true (local):

while wait() do
	if script.Parent.Parent.Part1.Value == true then
		game.ReplicatedStorage.ChangeBoolValues:FireServer(game.ReplicatedStorage.Part1Unlock,true)
	end
end
1 Like

where is this while loop??? on top or bottom

1 Like

while wait() do is basically while true do but it doesn’t timeout

edit: bottom

1 Like

try putting the loop in a spawn function

1 Like

Didn’t work… Maybe i could give you the RBLX file to you maybe fix it?

(also requires to you publish the game due datastore service)

1 Like

ok, but i wont be able to fix it till tomorrow but as soon as i wake up i will try my best to fix it

Sure thing, Reply to me tomorrow.

GameWhenRejoin.rbxl (29.1 KB)

1 Like

The main thing that I really see is that you’re not assigning your DataStore functions in pcalls to ensure that the data loads

Try this:

local datastore = game:GetService("DataStoreService"):GetDataStore("GameChanging")

game.Players.PlayerAdded:Connect(function(player)
	local Part1 = Instance.new("BoolValue")
	Part1.Parent = player
	Part1.Name = "Part1"
	
	local key = "user-" .. player.userId

	local storeditems 
    local success, whoops = pcall(function()
        storeditems = datastore:GetAsync(key)
    end)

    if success and storeditems then
        print("Data has loaded successfully!")
        Part1.Value = storeditems
    else
        warn("An error occured loading the data, or this is a new player!")
    end
end)

game.Players.PlayerRemoving:connect(function(player)
	local key = "user-" .. player.userId

    local success, whoops = pcall(function()
	    datastore:SetAsync(key, player.Part1.Value)
    end)

    if success then
        print("Data saved successfully!")
    else
        warn("An error occurred loading the data!", whoops)
    end
end)

that dont work dude its it always say An error occured loading the data, or this is a new player! even tho the player aint new

i think this will work also i dont think the local scripts are necessary

--this script is for the `data store thingy`
local datastore = game:GetService("DataStoreService"):GetDataStore("GameChanging")
game.Players.PlayerAdded:Connect(function(player)
	
	local Part1 = Instance.new("BoolValue")
	Part1.Parent = player
	Part1.Name = "Part1"
	local key = "user-" .. player.userId
	local storedItems
	local sucsess, stuff = pcall(function()
		storedItems = datastore:GetAsync(key)
	end)
	if sucsess then
		print(storedItems)
		if storedItems == true then
			Part1.Value = true
		end
		
		print("sucsess")
		print(Part1.Value)
		print("Gave stuff to ".. key)
	else
		print("no sucsess")
	end
end)

game.Players.PlayerRemoving:connect(function(player)

	items = {true}
	print(player.Part1.Value)
	local key = "user-" .. player.userId
	print(key, items)
	local sucsess, whoopsie = pcall(function()
		datastore:SetAsync(key, items)
	end)	

end)

The reason why it doesn’t work is cause I haven’t implemented a BindToClose function within it yet, which sometimes the PlayerRemoving Event won’t fire within Studio

local datastore = game:GetService("DataStoreService"):GetDataStore("GameChanging")

game.Players.PlayerAdded:Connect(function(player)
	local Part1 = Instance.new("BoolValue")
	Part1.Parent = player
	Part1.Name = "Part1"

	local key = "user-" .. player.userId

	local storeditems 
	local success, whoops = pcall(function()
		storeditems = datastore:GetAsync(key)
	end)

	if success and storeditems then
		print("Data has loaded successfully!")
		Part1.Value = storeditems
	else
		warn("An error occured loading the data, or this is a new player!", whoops)
	end
end)

local function SaveData(player)
	local key = "user-" .. player.userId

	local success, whoops = pcall(function()
		datastore:SetAsync(key, player.Part1.Value)
		print("Y u no work")
	end)
	

	print("Test")
	
	if success then
		print("Data saved successfully!")
	else
		warn("An error occurred loading the data!", whoops)
	end
end

game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		SaveData(Player)
	end
end)

I’m not sure you cant datastore boolean Values as it says that datastores can only store string values.

it still no work it say this too

13:30:37.258  An error occured loading the data, or this is a new player! nil  -  Server - Script:19
13:31:00.324  Disconnect from ::ffff:127.0.0.1|59484  -  Studio
13:31:00.434  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = user-1869110634  -  Studio
13:31:00.504  Y u no work  -  Server - Script:28
13:31:00.504  Test  -  Server - Script:32
13:31:00.504  Data saved successfully!  -  Server - Script:35
13:31:07.287  Y u no work  -  Server - Script:28
13:31:07.287  Test  -  Server - Script:32
13:31:07.287  Data saved successfully!  -  Server - Script:35`

Play the game again :roll_eyes: The data should save regardless

@RatiusRat You can, you just can’t save Instances within DataStores (Or Humanoid/Part/VehicleSeat Objects)

do you think the reason its not working for me is bc i test it in studio???

You probably put it in the wrong place

It should belong in ServerScriptService as a ServerScript, and API Services need to be turned on

It worked on my side I don’t know why it’s not working for you

the SereverScript is in server script service and APIs are on still dont know why it not work