Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to make a GUI appear on touch for a few seconds. Why doesn't it work?

SOLVED

Trying to make a GUI appear on the screen for a few seconds when a player hits a part. The scene is, the player goes through a teleporter, and once they go to the target teleport, they are forced to touch a invisible no collide brick and once they touch it a GUI appears on the screen for a few seconds.

debounce = false
script.Parent.Touched:Connect(function(hit)
	local frame = game.StarterGui.shaking.Frame
	frame.Visible = true
	wait(5)
	frame.Visible = false
	debounce = true
end)

4 comments

0

Well, the error here is indexing the frame in startergui. You need to make changes to gui in a player's PlayerGui

0

You'd have to get the player from the character which I don't know how to do but there should be tutorials.

0

player = game.Players:GetPlayerFromCharacter(hit.Parent) is how you want to go about doing that

0

And then just get the frame from playergui, could you construct that into an answer?

2 answers

✓ Accepted answer
debounce = false
script.Parent.Touched:Connect(function(hit)
    if debounce == false then
  	  debounce = true
   	 if hit.Parent:FindFirstChild("Humanoid") then
  		  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    		  local frame = player.PlayerGui.shaking.Frame
	  	  frame.Visible = true
 		   wait(5)
 		   frame.Visible = false

	end
   end
end)

Okay, you had some more errors in there that I corrected.

You have to check if the debounce is false so that it knows to continue, and then set it to true after you check if it's false.

You then need to check if whatever hit it has a humanoid, so that the code doesn't error if a random part touches it.

You then need to get the character's player, and set that to a variable(in this case the variable is called player)

You also need to change StarterGui to PlayerGui, since starterGui only clones its children when the player joins.

Lastly, you don't need the last debounce since it changes after it checks it, therefor not allowing it to run through the code again.

Hopefully this helped, and if it did please accept the answer! :D

3 comments

0

You keep it in startergui, you just dont mention starterGui in the code

0

Still doesn't work. Does it have to do with there being a label inside the frame? or maybe because the brick is nocollide?

0

Ahhh my bad. I see the error in this; ive edited the code

I ended up just using this script for the GUI. this seems to work well enough.

local debounce = false

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

script.Parent:clone().Parent = player.PlayerGui
wait(2)
debounce = false
player.PlayerGui.OnTouchGui:remove()
wait()
debounce = false
wait(5)
end
end


script.Parent.Parent.Touched:connect(onTouch) 

This discussion is preserved as it was. New answers, comments, and votes are closed.