Hey! I’ve been trying to make a gui visible and tween it on part touch. That works, however, making it tween back and set it to invisible again doesn’t. (Touched and TouchEnded functions.)
The error I’m getting is the exact same what I put as the title.
local debounce = false
script.Parent.Touched:Connect(function(hit)
local player = game.Players:FindFirstChild(hit.Parent.Name)
if debounce == false then
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
local gui = script.ElChapoShop
local clone = gui:Clone()
clone.Parent = player.PlayerGui
clone.ElChapoShop.Visible = true
clone.ElChapoShop:TweenPosition(UDim2.new(0.5,0,0.5,0),"Out","Sine", 1)
end
end
script.Parent.TouchEnded:Connect(function(hit)
local player = game.Players:FindFirstChild(hit.Parent.Name)
if debounce == true and player.PlayerGui:FindFirstChild("ElChapoShop") == false then
debounce = false
elseif debounce == true and player.PlayerGui:FindFirstChild("ElChapoShop") == true then
local gui = player.PlayerGui:FindFirstChild("ElChapoShop")
gui.ElChapoShop:TweenPosition(UDim2.new(0.5,0,1.5,0),"Out","Sine", 1)
wait(1)
gui:Destroy()
debounce = false
end
end)
end)
Use GetPlayerFromCharacter when getting the Player, you should also check if you are actually getting the Player with an if statement, because hit.Parent does not ensure a Model.
I see many issues in the code, I just did a small edition and added a couple of comments:
local debounce = false
script.Parent.Touched:Connect(function(hit)
if not debounce then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
local gui = script.ElChapoShop
local clone = gui:Clone()
clone.Parent = player.PlayerGui
clone.ElChapoShop.Visible = true
clone.ElChapoShop:TweenPosition(UDim2.new(0.5,0,0.5,0),"Out","Sine", 1)
end
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if debounce then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
if player:FindFirstChild("PlayerGui") then
if player.PlayerGui:FindFirstChild("ElChapoShop") then -- Check if theres a ScreenGui Called ElChapoShop?
debounce = false
else
local gui = player.PlayerGui:FindFirstChild("ElChapoShop")
gui.ElChapoShop:TweenPosition(UDim2.new(0.5,0,1.5,0),"Out","Sine", 1) -- Tween a "Frame" called ElChapoShop inside a ScreenGui called ElChapoShop too?...
task.wait(1) -- Wait until the Tween finished not a "random" 1 second
gui:Destroy()
debounce = false
end
end
end
end
end)