i made a script which checks if a button is pressed, what I want to do is check if the button is being currently pressed like :
if box is on button then its pressed
if box is not on button then its not pressed
this is the script
local button = script.Parent
local touchCD = false
local CollectionService = game:GetService("CollectionService")
local eventopen = game.ServerStorage.DoorOpen
local eventclose = game.ServerStorage.DoorClose
local currentlevel = game.ReplicatedStorage.CurrentLevel
local CM = require(game.ServerScriptService.CutsceneManager)
button.Touched:Connect(function(t)
if CollectionService:HasTag(t,"Grabbable") and t.CanCollide == true and touchCD == false then
local doors = game.Workspace["Lvl"..currentlevel.Value].Doors
touchCD = true
script.Parent.Pressed.Value = true
eventopen:Fire(script.Parent.ConnectedTo.Value)
print("pressed")
wait(2)
touchCD = false
end
end)
script.Parent.Pressed.Changed:Connect(function()
CM.animatecutscene({"talk:Good job, now you can pass :)"})
end)
Yes, you can use the Hovered event instead of Touched to achieve what you’re trying to do. The Hovered event fires when a part is being touched by another part or character, and it will continue to fire as long as the other part or character remains in contact with the button.
You can modify your script as follows:
local button = script.Parent
local CollectionService = game:GetService("CollectionService")
local eventopen = game.ServerStorage.DoorOpen
local eventclose = game.ServerStorage.DoorClose
local currentlevel = game.ReplicatedStorage.CurrentLevel
local CM = require(game.ServerScriptService.CutsceneManager)
button.Hovered:Connect(function(t)
if CollectionService:HasTag(t,"Grabbable") and t.CanCollide == true then
script.Parent.Pressed.Value = true
eventopen:Fire(script.Parent.ConnectedTo.Value)
print("pressed")
else
script.Parent.Pressed.Value = false
eventclose:Fire(script.Parent.ConnectedTo.Value)
print("released")
end
end)
script.Parent.Pressed.Changed:Connect(function()
CM.animatecutscene({"talk:Good job, now you can pass :)"})
end)
In this modified script, the Hovered event checks if the object touching the button has the “Grabbable” tag and is collidable. If it does, it sets the Pressed value of the button to true and opens the connected door. If the object stops touching the button, the Pressed value is set to false and the connected door is closed.
Note that there is no need for the touchCD variable in this implementation since the Hovered event will continue to fire as long as the object is in contact with the button.
I apologize for the confusion, I made a mistake. The Hovered event does not exist, but you can achieve similar functionality using a combination of the PartTouched and PartTouching events. Here’s an updated version of your script that should work as intended:
local button = script.Parent
local CollectionService = game:GetService("CollectionService")
local eventopen = game.ServerStorage.DoorOpen
local eventclose = game.ServerStorage.DoorClose
local currentlevel = game.ReplicatedStorage.CurrentLevel
local CM = require(game.ServerScriptService.CutsceneManager)
local function checkButtonStatus()
local touching = button:GetTouchingParts()
for _, part in pairs(touching) do
if CollectionService:HasTag(part,"Grabbable") and part.CanCollide == true then
return true
end
end
return false
end
button.PartTouched:Connect(function(part)
if CollectionService:HasTag(part,"Grabbable") and part.CanCollide == true then
script.Parent.Pressed.Value = checkButtonStatus()
eventopen:Fire(script.Parent.ConnectedTo.Value)
print("pressed")
end
end)
button.PartTouching:Connect(function(part)
script.Parent.Pressed.Value = checkButtonStatus()
if not checkButtonStatus() then
eventclose:Fire(script.Parent.ConnectedTo.Value)
print("released")
end
end)
script.Parent.Pressed.Changed:Connect(function()
CM.animatecutscene({"talk:Good job, now you can pass :)"})
end)
In that case, you can modify the checkButtonStatus() function to use the GetTouchingParts() method of the BasePart class to check if the button is being pressed:
local function checkButtonStatus()
local touching = button:GetTouchingParts()
for _, part in pairs(touching) do
if CollectionService:HasTag(part,"Grabbable") and part.CanCollide == true then
return true
end
end
return false
end
This function will return true if the button is being pressed by any parts that have the “Grabbable” tag and are collidable, and false otherwise.
You can then use this checkButtonStatus() function in the PartTouched and PartTouching events to set the Pressed value of the button and control the opening and closing of the connected door, as shown in the previous modified script.
I apologize for the confusion. The events PartTouched and PartTouching are not functions, they are events that are fired by the BasePart class when a part touches or stops touching the given part.
Here’s how you can use these events to check if the button is being pressed:
local button = script.Parent
local CollectionService = game:GetService("CollectionService")
local eventopen = game.ServerStorage.DoorOpen
local eventclose = game.ServerStorage.DoorClose
local currentlevel = game.ReplicatedStorage.CurrentLevel
local CM = require(game.ServerScriptService.CutsceneManager)
local function checkButtonStatus()
local touching = button:GetTouchingParts()
for _, part in pairs(touching) do
if CollectionService:HasTag(part,"Grabbable") and part.CanCollide == true then
return true
end
end
return false
end
button.Touched:Connect(function(part)
if CollectionService:HasTag(part,"Grabbable") and part.CanCollide == true then
script.Parent.Pressed.Value = checkButtonStatus()
eventopen:Fire(script.Parent.ConnectedTo.Value)
print("pressed")
end
end)
button.TouchEnded:Connect(function(part)
script.Parent.Pressed.Value = checkButtonStatus()
if not checkButtonStatus() then
eventclose:Fire(script.Parent.ConnectedTo.Value)
print("released")
end
end)
script.Parent.Pressed.Changed:Connect(function()
CM.animatecutscene({"talk:Good job, now you can pass :)"})
end)
In this script, the Touched event is fired when a part touches the button, and the TouchEnded event is fired when a part stops touching the button.
The checkButtonStatus() function is used to determine if any parts that have the “Grabbable” tag and are collidable are currently touching the button. If there are, the function returns true to indicate that the button is being pressed. If not, it returns false.
The Touched event handler checks if the part that touched the button has the “Grabbable” tag and is collidable. If it does, it sets the Pressed value of the button to true and opens the connected door.
The TouchEnded event handler checks if any parts that have the “Grabbable” tag and are collidable are still touching the button. If there are, it sets the Pressed value of the button to true. If not, it sets the Pressed value of the button to false and closes the connected door.