So the problem is that the script only works once. My GUI script did what it did by disabling the GUI once done but after making the GUI to false the touch script stops working
local part = script.Parent
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local playerGui = player:WaitForChild("PlayerGui")
local questionGui = playerGui:FindFirstChild("Question1")
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
humanoidRootPart.Anchored = true
questionGui.Enabled = true
end)
So I modified your script to make some to reset the gui to keep it working. Feel free to make any changes.
local part = script.Parent
local debounce = false
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local playerGui = player:WaitForChild("PlayerGui")
local questionGui = playerGui:WaitForChild("Question1")
if questionGui.Enabled == false and debounce == false then
debounce = true
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
humanoidRootPart.Anchored = true
questionGui.Enabled = true
task.wait(2) --Time to answer the question
humanoidRootPart.Anchored = false
questionGui.Enabled = false
task.wait(2) --Cooldown for the player to step on the part
debounce = false
end
end)
It sounds like the issue is that once the GUI is disabled, the touch script is not reconnected to the Touched event. To fix this, you can move the part.Touched:Connect() statement outside of the event handler for the GUI. Here’s an updated version of your script that should work:
local part = script.Parent
local function onPartTouched(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local playerGui = player:WaitForChild("PlayerGui")
local questionGui = playerGui:FindFirstChild("Question1")
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
humanoidRootPart.Anchored = true
questionGui.Enabled = true
part.Touched:Connect(onPartTouched) -- Reconnect the event handler
end
part.Touched:Connect(onPartTouched) -- Connect the event handler initially
With this change, the onPartTouched function is called every time the part is touched, and the event handler is reconnected after the GUI is displayed, so the script should work multiple times.
it works but not what I exactly want it to work since I’m making a racing game where players have to answer to advance to the next question until finish but the thing is that this script also gives cooldown to the other questions(touch parts) but thanks for helping