Hi! I’m working on a sci-fi puzzle-type game, and I’m having trouble with a button on the floor that activates when stepped on/item is placed on. It would continuously flicker on and off, acting as if the item has left the button when it has stayed put. To correct this, I’ve added a lot of (probably unnecessary) conditions and code to the script. I’m not very good at scripting, and there are still some issues. I was wondering if somebody could clean up this code and prevent the weird flickering thing.
Here’s the code inside the button:
Inside the model containing the button
local buttonpart = script.Parent.Button
local buttonpressed = script.Parent.ButtonPressed
local hitbox = script.Parent.Hitbox
local state = false
local db = false
local currentobject = nil
hitbox.Touched:Connect(function(hit)
if currentobject == nil then
if hit.Name == "HumanoidRootPart" or hit.Name == "Cube" or hit.Name == "HeavyObject" then
local heavy = hit.Parent:FindFirstChild("ButtonHeavy") or hit.Parent.Parent:FindFirstChild("ButtonHeavy")
local onbutton = hit.Parent:FindFirstChild("OnButton") or hit.Parent.Parent:FindFirstChild("OnButton")
if heavy and onbutton then
if heavy.Value == true then
if state == false and onbutton.Value == false then
if db == false then
db = true
currentobject = hit
state = true
wait(0.25)
db = false
script.Parent.Activated.Value = true
hit.Parent.OnButton.Value = true
buttonpart.CanCollide = false
buttonpart.Transparency = 1
buttonpressed.Transparency = 0
buttonpressed.PointLight.Enabled = true
buttonpart.StepOn:Play()
for i,player in pairs(game.Players:GetPlayers()) do
game.ReplicatedStorage.VibrateGamepad:FireClient(player, Enum.VibrationMotor.Small, 0.5, 0.2)
end
if hit.Name == "Cube" then
hit.DecalPart1.Decal1.Color3 = BrickColor.new("Bright green").Color
hit.DecalPart1.Decal2.Color3 = BrickColor.new("Bright green").Color
hit.DecalPart2.Decal1.Color3 = BrickColor.new("Bright green").Color
hit.DecalPart2.Decal2.Color3 = BrickColor.new("Bright green").Color
hit.DecalPart3.Decal1.Color3 = BrickColor.new("Bright green").Color
hit.DecalPart3.Decal2.Color3 = BrickColor.new("Bright green").Color
end
end
end
end
end
end
end
end)
hitbox.TouchEnded:Connect(function(hit)
if currentobject == hit then
if state == true then
if db == false then
if hit.Name == "HumanoidRootPart" or hit.Name == "Cube" or hit.Name == "HeavyObject" then
if hit.Name == "Cube" or hit.Name == "HeavyObject" then
if hit.Parent.BeingCarried.Value == true then
db = true
state = false
wait(0.25)
db = false
script.Parent.Activated.Value = false
hit.Parent.OnButton.Value = false
buttonpart.CanCollide = true
buttonpart.Transparency = 0
buttonpressed.Transparency = 1
buttonpressed.PointLight.Enabled = false
buttonpart.StepOff:Play()
currentobject = nil
hit.DecalPart1.Decal1.Color3 = BrickColor.new("Smoky grey").Color
hit.DecalPart1.Decal2.Color3 = BrickColor.new("Smoky grey").Color
hit.DecalPart2.Decal1.Color3 = BrickColor.new("Smoky grey").Color
hit.DecalPart2.Decal2.Color3 = BrickColor.new("Smoky grey").Color
hit.DecalPart3.Decal1.Color3 = BrickColor.new("Smoky grey").Color
hit.DecalPart3.Decal2.Color3 = BrickColor.new("Smoky grey").Color
end
else
db = true
state = false
wait(0.25)
db = false
script.Parent.Activated.Value = false
hit.Parent.OnButton.Value = false
buttonpart.CanCollide = true
buttonpart.Transparency = 0
buttonpressed.Transparency = 1
buttonpressed.PointLight.Enabled = false
buttonpart.StepOff:Play()
currentobject = nil
end
end
end
end
end
end)
If you have any solutions, I would greatly appreciate them. Thanks!