Setting to disable attribute not working on new players

Essentially, I have a setting UI that allows you to disable a Proximity Prompt that is in all eligible player’s HumanoidRootPart. In this case, only people who are above a certain rank in the group will have the Proximity Prompt. The way this tipping system was made is that it runs on RunService.HeartBeat to constantly check if an attribute in the player is true or false and from there, it determines whether to create/keep the Proximity Prompt or not.

For this setting that I’m making, it will allow all players to choose to hide the Proximity Prompt of all the people that have it by changing the attribute to false. My issue is that it seems to be working for all the current players, but when a new player joins it won’t disable theirs even though I wrote it so that it should.

Here’s my code:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local currentPlayerConnections = {}

local button = script.Parent
local enabled = button:WaitForChild("Enabled")
local disabled = script.Parent:WaitForChild("Disabled")
disabled.Enabled = true
enabled.Enabled = false

local function UpdateTipping(Character)
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

	if HumanoidRootPart and HumanoidRootPart:FindFirstChild("TippingPrompt") then
		Players:GetPlayerFromCharacter(Character):SetAttribute("TipsEnabled", not enabled.Enabled)
	end
end

enabled:GetPropertyChangedSignal("Enabled"):Connect(function() 
	for _, player in Players:GetPlayers() do 
		local Character = player.Character or player.CharacterAdded:Wait()

		UpdateTipping(Character)
	end
end)

button.Activated:Connect(function()
	local currentValue = enabled.Enabled
	enabled.Enabled = not currentValue 
	
	local disabledcurrentVal = disabled.Enabled
	disabled.Enabled = not disabledcurrentVal
end)

---

local function CreateEventConnections(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	UpdateTipping(Character) 

	if not currentPlayerConnections[player] then
		currentPlayerConnections[player] = player.CharacterAdded:Connect(UpdateTipping)
		
	end
end

for _, player in Players:GetPlayers() do
	CreateEventConnections(player)
end

Players.PlayerAdded:Connect(function(player)
	CreateEventConnections(player)
end)

Players.PlayerRemoving:Connect(function(player)
	currentPlayerConnections[player]:Disconnect()
	currentPlayerConnections[player] = nil
end)

It seems like you are not passing in the character under the character added event. And not handling the disconnect.
Try something like this:

local function CreateEventConnections(player)
	
	if currentPlayerConnections[player] then
		currentPlayerConnections[player]:Disconnect()
	end
	
	currentPlayerConnections[player] = player.CharacterAdded:Connect(function()
		local char = player.Character or player.CharacterAdded:Wait()
		UpdateTipping(char)
	end)
end
1 Like

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