Change my script to a touch one

so i wanted to change my click to get text gui to a touch to get gui one, don’t know how to do it tho
here’s the click script:

function onClick(click)
for i,v in pairs (script.Parent:GetChildren()) do
	if v.ClassName == "ScreenGui" then
	local c = v:Clone()
	c.Parent = click.PlayerGui
	end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)
2 Likes

Try this

function onTouched(hit)
if hit and hit.Parent then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("ScreenGui") then
local c = v:Clone()
c.Parent = player.PlayerGui
end
end
end
end
end

script.Parent.Touched:Connect(onTouched)

I don’t think so, here is what he said

i added the debounce and it didn’t work ;-;

function onTouched(hit)
	
	if hit and hit.Parent then
		local debounce = false
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		wait (20)
		local debounce = true
		if player then
			for i, v in pairs(script.Parent:GetChildren()) do
				if v:IsA("ScreenGui") then
					local c = v:Clone()
					c.Parent = player.PlayerGui
				end
			end
		end
	end
end

script.Parent.Touched:Connect(onTouched)

Do not add debounce inside the function.
Use it outside the function.
If you wanna overwrite the debounce don’t use local again

One more thing, isn’t 20 second took so long?

well i only like want it to do like the tutorial of a tycoon game or something wheer you touch it and a typewriter gui pops up and stuff
(i only want it to appear once)

Try this

local debounce = false

function onTouched(hit)
if hit and hit.Parent then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce then
debounce = true
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("ScreenGui") then
local c = v:Clone()
c.Parent = player.PlayerGui
end
end
wait(20)
debounce = false
end
end
end

script.Parent.Touched:Connect(onTouched

it repeats the text every 20 seconds ;-;
i added the ) for the ontouched

I’m not too sure, probably create other topic? This one is solved

too long

-- i added debounce since i read previous replies

local deb = false

function onTouched(hit)
  if deb then return end
  deb = true
  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  if player then
    local v = script.Parent:FindFirstChildOfClass('ScreenGui')
    if not v then return end -- returns if it doesn't exist
    local c = v:Clone()
    c.Parent = player:WaitForChild('PlayerGui')
  end
  wait(20)
  deb = false
end

script.Parent.Touched:Connect(onTouched)
1 Like

Lol, I’m on my phone. Which is pretty hard.