You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
- Sorry if the title is unclear. I want to write a code that uses ContextActionService to unbind a key depending on an attribute of the player.
- What is the issue? Include screenshots / videos if possible!
- The player has an attribute named “C1”. For context, C1 is set to true when the player switches to the character that is in slot 1 (in better words, player presses 1 and C1 becomes true). In the code provided below, it checks to see if the C1 folder is empty OR if C1 is true to unbind the 1 key. However, it simply just doesnt work.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
- I tried using stringValue instances, but the same problem occurred and it came along with the “attempt to index nil” error and I’d rather use attributes anyway.
I basically don’t want the player to press 1 if “C1” is already set to true. What should I do so that this script checks the attribute?
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 ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
local folderC1 = ReplicatedStorage.Characters:WaitForChild("C1") -- Assuming "C1" is the folder
local function onAction(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
-- Perform action when "1" key is pressed down
print("Cannot Swap")
end
end
local function updateActionBinding()
if #folderC1:GetChildren() == 0 or plr:GetAttribute("C1") then
-- Bind if folder is not empty
ContextActionService:BindAction("Unbind", onAction, false, Enum.KeyCode.One)
else
-- Unbind if folder is empty
ContextActionService:UnbindAction("Unbind")
end
end
-- Initial update
updateActionBinding()
-- Listen for changes in folder content
folderC1.ChildAdded:Connect(updateActionBinding)
folderC1.ChildRemoved:Connect(updateActionBinding)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.