This is supposed to trigger a screen gui for the player when they touch a brick, keep the gui up while they remain touching that brick, and then remove the gui once the touch ends. For the most part, it works. However, occassionally, the gui will flicker while they run into the brick, and even more rarely sometimes the gui gets stuck on the screen, even though the player has long since walked away from the brick.
Does anyone have any advice on how I can solve this? I feel like I must not be using debounces properly, but I'm having a very hard time figuring out what exactly I'm doing wrong.
script.Parent.Transparency = 1
local inRange = false
local isTouched = false
local store = script.StoreScript
local debounce = false
local eDebounce = false
local range = false
--Remote Events
local storeGuiEvent = Instance.new("RemoteEvent")
storeGuiEvent.Parent = game.ReplicatedStorage
storeGuiEvent.Name = "storeGuiEvent"
local storeGuiEventEnd = Instance.new("RemoteEvent")
storeGuiEventEnd.Parent = game.ReplicatedStorage
storeGuiEventEnd.Name = "storeGuiEventEnd"
local createStoreRequest = Instance.new("RemoteEvent")
createStoreRequest.Parent = game.ReplicatedStorage
createStoreRequest.Name = "CreateStoreRequest"
function onTouch(HIT)
if inRange == false then
local H = HIT.Parent:FindFirstChild("Humanoid")
if range == false then
range = true
if H and not debounce and range == true then
debounce = true
character = HIT.Parent
Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
print("Fire")
storeGuiEvent:FireClient(Player)
debounce = false
inRange = true
end
end
end
end
function endTouch(END)
local E = END.Parent:FindFirstChild("Humanoid")
if E and not eDebounce then
eDebounce = true
if Player == nil then
return
else
storeGuiEventEnd:FireClient(Player)
inRange = false
end
eDebounce = false
range = false
end
end
function onCreateStoreRequested(player)
store:Clone().Parent = character
end
script.Parent.Touched:connect(onTouch)
script.Parent.TouchEnded:connect(endTouch)
createStoreRequest.OnServerEvent:Connect(onCreateStoreRequested)

1 comment
Sorry it's taken me so long to respond! This doesn't quite explain why the issue occasionally happens... but it does give me a viable solution to deal with it when it does happen. Instead of destroying the script, I just call the Gui End event that I made after the wait. Works really well, thanks!