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

When somone touches the chest nothing happens,I want it to display a gui, What is the problem?

I have a part surrounding the chest and then a script inside of it. But Nothing happens. I want the GUI to appear and then if you walk to it again it gets closed.

local storage = game:GetService("ServerStorage")
local gui = storage.CarUI
local part = workspace.ChestTrigger
local deb = false
local path = game.Players.LocalPlayer.PlayerGui.CarUI

part.Touched:connect(function()
	if (deb == false) then
		gui:Clone().Parent = game.Players.LocalPlayer.PlayerGui
		wait(2)
		deb = true
	elseif (deb == true) then
		path:Destroy()
		deb = false
	end
end)

PLEASE help me.

1 answer

✓ Accepted answer

I suggest making a button on your gui to close it, because Touching the part will be inaccurate.

  • You cannot access the LocalPlayer from server scripts. You have to retrieve the player based on who touched the Part using the GetPlayerFromCharacter function.
local gui = game.ServerStorage.CarUI
local part = workspace.ChestTrigger
local deb = false

part.Touched:connect(function(hit) --'hit' is the part that touched
	if not db then
		db = true
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		--If player exists, and gui doesn't
		if plr and not plr.PlayerGui:FindFirstChild(gui.Name) then
			gui:Clone().Parent = plr.PlayerGui --Clone it
		end
		db = false
	end
end)

A close button for a gui would use the MouseButton1Down event in a LocalScript, and look like this:

script.Parent.MouseButton1Down:Connect(function() --button clicked
	script.Parent.Parent.Parent:Destroy() --destroy ScreenGui
end)

Hierarchy of UI:

ScreenGui

Frame

TextButton

3 comments

0

Thanks :)

0

Can't I do a touch ended function?

0

You can but due to physics Touched and TouchEnded events would fire a bunch. If you really don't want an 'x' button on your UI, PM me on discord i'll walk you through what you can do alternatively.

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