Help to optimize and fix the code

  1. I need to make it so that the player can activate the button activation and save once. But I don’t really understand how to do
    this, there is an error in my code and when activating all players it writes to the table and only for one the button turns on

image

It would be great if someone would tell me what’s wrong and how to fix it, because I haven’t been able to find a solution for a week

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Players = game:GetService("Players")
local Po1a = workspace.Folder.PointsTp:WaitForChild("Po1a")
local Po2b = workspace.Folder.PointsTp:WaitForChild("Po2b")
local Po3c = workspace.Folder.PointsTp:WaitForChild("Po3c")
local Po4d = workspace.Folder.PointsTp:WaitForChild("Po4d")
local Po5e = workspace.Folder.PointsTp:WaitForChild("Po5e")
local Po6f = workspace.Folder.PointsTp:WaitForChild("Po6f")
local Po7g = workspace.Folder.PointsTp:WaitForChild("Po7g")
local Po8h = workspace.Folder.PointsTp:WaitForChild("Po8h")
local Po9i = workspace.Folder.PointsTp:WaitForChild("Po9i")
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("PlayerVisibility")

local function saveVisible(player, buttonName, isVisible)
	local key = buttonName .. "_".. player.UserId
	dataStore:SetAsync(key, isVisible)
	print("Saved " .. buttonName)
end

local function loadVisible(player, buttonName)
	local key = buttonName .. "_" .. player.UserId
	local success, isVisible = pcall(function()
		return dataStore:GetAsync(key)
	end)
	if success then
		print("Loaded")
		return isVisible or false
	else
		return false
	end
end

Players.PlayerAdded:Connect(function(player)
	local buttonNames = {"Po1", "Po2", "Po3","Po4","Po5","Po6","Po7","Po8","Po9"}
	for _, buttonName in pairs(buttonNames) do
		local isVisible = loadVisible(player, buttonName)
		local frame = player.PlayerGui:WaitForChild("TPmeny").Frame
		if frame and frame[buttonName] then
			frame[buttonName].Visible = isVisible
		end
	end
end)

local activatedPlayers1 = {}
local activatedPlayers2 = {}
local activatedPlayers3 = {}
local activatedPlayers4 = {}
local activatedPlayers5 = {}
local activatedPlayers6 = {}
local activatedPlayers7 = {}
local activatedPlayers8 = {}
local activatedPlayers9 = {}

game.Players.PlayerAdded:Connect(function(player) 
	
	Po1a.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player and not activatedPlayers1[player.UserId] then
			activatedPlayers1[player.UserId] = true
			
			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po1.Visible = true
				saveVisible(player, "Po1", true)
			end
			print(player.Name .. " activated Part1")
		end
	end)

	Po2b.Touched:Connect(function(hit)
		if player and not activatedPlayers2[player.UserId] then
			activatedPlayers2[player.UserId] = true
			
			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po2.Visible = true
				saveVisible(player, "Po2", true)
			end
			print(player.Name .. " activated Part2")
		end
	end)

	Po3c.Touched:Connect(function(hit)
		if player and not activatedPlayers3[player.UserId] then
			activatedPlayers3[player.UserId] = true
			
			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po3.Visible = true
				saveVisible(player, "Po3", true)
			end
			print(player.Name .. " activated Part3")
		end
	end)
	
	Po4d.Touched:Connect(function(hit)
		if player and not activatedPlayers4[player.UserId] then
			activatedPlayers4[player.UserId] = true

			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po4.Visible = true
				saveVisible(player, "Po4", true)
			end
			print(player.Name .. " activated Part4")
		end
	end)
	
	Po5e.Touched:Connect(function(hit)
		if player and not activatedPlayers5[player.UserId] then
			activatedPlayers5[player.UserId] = true

			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po5.Visible = true
				saveVisible(player, "Po5", true)
			end
			print(player.Name .. " activated Part5")
		end
	end)
	
	Po6f.Touched:Connect(function(hit)
		if player and not activatedPlayers6[player.UserId] then
			activatedPlayers6[player.UserId] = true

			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po6.Visible = true
				saveVisible(player, "Po6", true)
			end
			print(player.Name .. " activated Part6")
		end
	end)
	
	Po7g.Touched:Connect(function(hit)
		if player and not activatedPlayers7[player.UserId] then
			activatedPlayers7[player.UserId] = true

			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po7.Visible = true
				saveVisible(player, "Po7", true)
			end
			print(player.Name .. " activated Part7")
		end
	end)
	
	Po8h.Touched:Connect(function(hit)
		if player and not activatedPlayers8[player.UserId] then
			activatedPlayers8[player.UserId] = true

			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po8.Visible = true
				saveVisible(player, "Po8", true)
			end
			print(player.Name .. " activated Part8")
		end
	end)
	
	Po9i.Touched:Connect(function(hit)
		if player and not activatedPlayers9[player.UserId] then
			activatedPlayers9[player.UserId] = true

			if hit.Parent == player.Character then
				player:WaitForChild('PlayerGui').TPmeny.Frame.Po9.Visible = true
				saveVisible(player, "Po9", true)
			end
			print(player.Name .. " activated Part9")
		end
	end)
	
end)

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild('Humanoid').Died:Connect(function ()
		wait(4)
			local buttonNames = {"Po1", "Po2", "Po3","Po4","Po5","Po6","Po7","Po8","Po9"}
			for _, buttonName in pairs(buttonNames) do
				local isVisible = loadVisible(player, buttonName)
				local frame = player.PlayerGui:WaitForChild("TPmeny").Frame
				if frame and frame[buttonName] then
					frame[buttonName].Visible = isVisible
				end
			end
		end)
	end)
end)
2 Likes

What is the reason you are using PlayerAdded so frequently?

Why is it needed?

3 Likes

it’s just that I’m a beginner and I’m not very good at evaluating what is needed and what is not, and yes, it would probably be possible to use PlayerAdded once or twice

It looks like you are having trouble using the PlayerAdded function.

I restructured the script you wrote above to better use sthe PlayerAdded function. It may look very different, but everything is still there (I think).

It should help eliminate any problems with the PlayerAdded function.

Try it and see if it helps you identify the problem you are having with the datastore.




--===========
-- VARIABLES
--===========

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("PlayerVisibility")
local activatedPlayers1 = {}
local activatedPlayers2 = {}
local activatedPlayers3 = {}
local activatedPlayers4 = {}
local activatedPlayers5 = {}
local activatedPlayers6 = {}
local activatedPlayers7 = {}
local activatedPlayers8 = {}
local activatedPlayers9 = {}



--==============
-- SAVE VISIBLE
--==============

local function saveVisible(player, buttonName, isVisible)
	local key = buttonName .. "_".. player.UserId
	dataStore:SetAsync(key, isVisible)
	print("Saved " .. buttonName)
end



--==============
-- LOAD VISIBLE
--==============

local function loadVisible(player, buttonName)
	local key = buttonName .. "_" .. player.UserId
	local success, isVisible = pcall(function()
		return dataStore:GetAsync(key)
	end)
	if success then
		print("Loaded")
		return isVisible or false
	else
		return false
	end
end



--=============
-- TOUCH PARTS
--=============

-- gather all the parts in the folder and apply the part.Touched function to them

for _, part in pairs(workspace.Folder.PointsTp:GetChildren()) do

	local CanTouch = true

	part.Touched:Connect(function(hit)
		if CanTouch then
			CanTouch = false
			if Players:FindFirstChild(hit.Parent.Name) then -- it is the player's character
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if player and not activatedPlayers1[player.UserId] then

					activatedPlayers1[player.UserId] = true
					player:WaitForChild('PlayerGui').TPmeny.Frame[part.Name].Visible = true -- pick up the name of the touched part
					saveVisible(player, part.Name, true)
					print(player.Name .."activated".. part.Name)

				end
			end
			task.wait(1) -- delay prevents multiple hits (since CanTouch is not yet true)
			CanTouch = true
		end
	end)

end



--===================
-- RECALL VISIBILITY
--===================

-- make this a named function so you can call it when you want it

local buttonNames = {"Po1", "Po2", "Po3","Po4","Po5","Po6","Po7","Po8","Po9"}

local function RecallVisiblity(player)

	for _, buttonName in pairs(buttonNames) do
		local isVisible = loadVisible(player, buttonName)
		local frame = player.PlayerGui:WaitForChild("TPmeny").Frame
		if frame and frame[buttonName] then
			frame[buttonName].Visible = isVisible
		end
	end

end


--==============
-- PLAYER ADDED
--==============

-- executes only when player joins

local Players = game:GetService("Players")
local RESPAWN_DELAY = 5
Players.CharacterAutoLoads = false


local function PlayerAdded(player)

	print(player.Name .. "'s player has been added")
	
	RecallVisiblity(player)
	
	
	
	--=================
	-- CHARACTER ADDED
	--=================
	
	-- executes each time character is added
	
	local function CharacterAdded(character)

		print(player.Name .. "'s character has been added")
		
		RecallVisiblity(player)

		
		
		--================
		-- CHARACTER DIED
		--================
		
		-- executes seach time character dies

		local humanoid = character:WaitForChild("Humanoid")

		local function CharacterDied()
			
			print(player.Name .. "'s character died")

			task.wait(RESPAWN_DELAY)
			player:LoadCharacter()

		end
		humanoid.Died:Connect(CharacterDied)
		
		
		
		--====================
		-- CHARACTER REMOVING
		--====================

		-- happens just before the character respawns
		
		player.CharacterRemoving:Connect(function()
			
			print(player.Name .. "'s character removed")

		end)

	end
	player.CharacterAdded:Connect(CharacterAdded)

	player:LoadCharacter()
	
	

end
for _, player in Players:GetPlayers() do
	task.spawn(PlayerAdded, player)
end
Players.PlayerAdded:Connect(PlayerAdded)



--=================
-- PLAYER REMOVING
--=================

-- executes only when player leaves games

local function PlayerRemoving(player)

	print(player.Name .. "'s player is removing...")

end
Players.PlayerRemoving:Connect(PlayerRemoving)

1 Like

I do not know why, but now I am having some problems starting the game, but with my code, the game starts

It is looking for TPmeny in the PlayerGui on line 95.

If the name is misspelled or if it is not in that location then it will give you a warning like you see in the output to tell you where to look to fix the problem.

image
the name is correct, but the game seems to be loading and cannot be added to PlayerGui TPmeny

It’s like the game is loading endlessly

Remove line 119:

RecallVisiblity(player)

See if it loads then.

1 Like

I don’t know why, but after restarting Roblox Studio, everything worked

Awesome.

Are the datastore parts and the visibility working as you expected?

1 Like

unfortunately, it only works for the first button and I changed the names of the buttons in the code and in the explorer
at least in the team test

Is the print line you added updating with a new item each time one is touched?

print(player.Name .."activated".. part.Name)

thanks, everything works, I’ve probably been doing that code for a week and it didn’t work out very well. but this only means that I need to grow up as a programmer

Glad to have helped.

Now you have a solid chunk of code for how to handle PlayerAdded.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.