Scripts ignoring :GetAttribute() values from a Configuration object and going straight to executing

Hello, this is my first time posting on the developer forum, so apologies if I get anything wrong.

What am I trying to achieve?

So, I am making a game with my friend where we host competitions for specific prizes in; and so I would like to be able to disable options using a script.

So for example, I would like to disable spectating during hide and seek competitions so contestants cannot see where other contestants are.

What have I tried?

I tried using BoolValues in a folder but I have still been coming across the same error, and I haven’t found any solutions for this problem on the developer forum.

What is the code relating to this issue?

There is a custom commands script in the game for setting “on” and “off” values for the attributes of the Configuration object, it does change the values to either “true” or “false” (I found out because I printed the attribute)

Note: All the attributes in the Configuration object are set to the boolean value.

Attributes for the Configuration object.

Firstly, here is the script for the custom commands: (Edited to only relate to the specific issues)

local admins = {"BertNB", "baconboifanlol", "mepoorso", "Bapak_adelio", "ViMau007"}
eventSettings = game.ReplicatedStorage:WaitForChild("EventSettings")

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(c)
		if table.find(admins, plr.Name) ~= nil then
			local split = string.split(c, " ")
			if split[1] == "!nukes" then
				if split[2] == "on" then
					eventSettings:SetAttribute("Nukes_Enabled", true)
				end
				if split[2] == "off" then
					eventSettings:SetAttribute("Nukes_Enabled", false)
				end
			end
			if split[1] == "!spectate" then
				if split[2] == "on" then
					eventSettings:SetAttribute("Spectating_Enabled", true)
				end
				if split[2] == "off" then
					eventSettings:SetAttribute("Spectating_Enabled", false)
				end
			end
			if split[1] == "!ghost" then
				if split[2] == "on" then
					eventSettings:SetAttribute("Ghost_Mode_Enabled", true)
				end
				if split[2] == "off" then
					eventSettings:SetAttribute("Ghost_Mode_Enabled", false)
				end
			end
		end
	end)
end)

Now for the scripts that are ignoring the :GetAttribute() values from the Configuration object and going straight to executing:

This script is a ‘LocalScript’ in a spectating GUI for detecting if the Spectating_Enabled attribute is true.

cam = game.Workspace.CurrentCamera

local bar = script.Parent.Bar
local title = bar.Title
local prev = bar.Previous
local nex = bar.Next
local button = script.Parent.Button
local spectateEnabled = game.ReplicatedStorage:WaitForChild("EventSettings"):GetAttribute("Spectating_Enabled")

function get()
	for _,v in pairs(game.Players:GetPlayers())do
		if v.Name == title.Text then
			return(_)
		end
	end
end


local debounce = false
button.MouseButton1Click:connect(function()
	if spectateEnabled == true then
		if debounce == false then debounce = true
			bar:TweenPosition(UDim2.new(.5,-100,0.88,-50),"In","Linear",1,true)
			pcall(function()
				title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
			end)
		elseif debounce == true then debounce = false
			pcall(function() cam.CameraSubject = game.Players.LocalPlayer.Character.Humanoid end)
			bar:TweenPosition(UDim2.new(-1,-100,0.88,-50),"In","Linear",1,true)
		end
	end
end)

prev.MouseButton1Click:connect(function()
	if spectateEnabled == true then
		wait(.1)
		local players = game.Players:GetPlayers()
		local num = get()
		if not pcall(function() 
				cam.CameraSubject = players[num-1].Character.Humanoid
			end) then
			cam.CameraSubject = players[#players].Character.Humanoid
		end
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	end
end)

nex.MouseButton1Click:connect(function()
	if spectateEnabled == true then
		wait(.1)
		local players = game.Players:GetPlayers()
		local num = get()
		if not pcall(function() 
				cam.CameraSubject = players[num+1].Character.Humanoid
			end) then
			cam.CameraSubject = players[1].Character.Humanoid
		end
		pcall(function()
			title.Text = game.Players:GetPlayerFromCharacter(cam.CameraSubject.Parent).Name
		end)
	end
end)

Here is a ‘LocalScript’ in a GUI for exploding everyone in the server. (nuking)

----- Variables -----

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local nukesOn = ReplicatedStorage:WaitForChild("EventSettings"):GetAttribute("Nukes_Enabled")

local player = Players.LocalPlayer
local nukeButton = script.Parent
local oldText = nukeButton.Text
local nukeProductId = ReplicatedStorage:WaitForChild("NukeServer"):GetAttribute("ProductID")

----- Events -----

nukeButton.Activated:Connect(function()
	if nukesOn == true then
		MarketplaceService:PromptProductPurchase(player, nukeProductId)
	else
		script.Parent.Text = "sorry bud.. nukes are disabled!"
		task.wait(1)
		script.Parent.Text = oldText
	end
end)

Now finally, here is a server-sided script for making spectators ghosts (basically them being able to pretend like they’re alive to not bore them while they are waiting to be in a competition to be brought back)

script.Parent.ClickDetector.MouseClick:Connect(function(p)
	if game.ReplicatedStorage.EventSettings:GetAttribute("Nukes_Enabled") == true and p.Team == game.Teams.Spectators and not p.Backpack:FindFirstChild("Fly") or not p.Character:FindFirstChild("Fly") then
		game.ServerStorage.Fly:Clone().Parent = p.Backpack
		p.Character.Humanoid.MaxHealth = math.huge
		p.Character.Humanoid.Health = math.huge
		for i,v in p.Character:GetChildren() do
			if v:IsA("Part") == true and v.Name ~= "HumanoidRootPart" then
				v.Transparency = 0.6
			end
			if v:IsA("Accessory") == true then
				v:FindFirstChild("Handle"):FindFirstChildOfClass("SpecialMesh").TextureId = ""
				v:FindFirstChild("Handle").Transparency = 0.6
				v:FindFirstChild("Handle").Color = p.Character.Head.Color
			end
		end
	end
end)

Thank you for reading.

1 Like

So the issue is that even when attributes are changed on the server/through chat commands, the values in the localscripts don’t update correctly?

In the first and second scripts, the event attributes are assigned to variables near the beginning of each:

local spectateEnabled = game.ReplicatedStorage:WaitForChild("EventSettings"):GetAttribute("Spectating_Enabled")
--
local nukesOn = ReplicatedStorage:WaitForChild("EventSettings"):GetAttribute("Nukes_Enabled")

so when the value in the attribute updates, the variable doesn’t change. Instead, you could use the attributes directly so the script checks every time (like if EventSettings:GetAttribute("Spectating_Enabled") == true, or wrap them in a getter function like

local function getSpectateEnabled()
    return EventSettings:GetAttribute("Spectating_Enabled")
end

and then comparing with getSpectateEnabled() == true.

If this is the problem then the third script looks like it should work fine though.

Thank you so much, I didn’t need the return function for the server script (I screwed up with my custom commands), however it worked for the local scripts! Will keep in mind!

1 Like

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