How can my gui clone script be improved?

I have this free model shop menu that I sort of understand, but I’d like it to work differently and I’m not sure how to go about it. Basically how it would work in my mind, is that whether it’s on or off is based off a bool value that could also be changed by an event or something. Any ideas on how I could do that?

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			if not plr.PlayerGui:FindFirstChild("GUIClonedfromtouchblock") then
				local clonedgui = script.Parent:FindFirstChildOfClass("ScreenGui"):Clone()
				clonedgui.Name = "GUIClonedfromtouchblock"
				clonedgui.Parent = plr.PlayerGui
				script.Parent.TouchEnded:Connect(function(hit2)
					if hit == hit2 then
						game.Debris:AddItem(clonedgui,0)
					end
				end)
			end
		end
	end
end)

you could get the player’s name and then put this on it

local remoteevent = game.ReplicatedStorage.RemoteEvent-------------------the location of your remote event

remoteevent:FireClient(plrname, trueorfalse)----------------------------------------this fires a event to the player with the same name

and then you put this in a local script inside your gui and modify it with your variables

local gui = script.Parent --------------in this case u put you gui location

local remoteevent = game.ReplicatedStorage.RemoteEvent-------------------the location of your remote event

remoteevent.OnClientEvent:Connect(function(plr, action)
	if action == true then
		gui.Enabled = true
	else
		if action == false then
			gui.Enabled = false
		end
	end
end)

you should put the fireclient after cloning the gui

I don’t think you need the if action == false then part

		local gui = script.Parent --------------in this case u put you gui location

		local remoteevent = game.ReplicatedStorage.RemoteEvent-------------------the location of your remote event

		remoteevent.OnClientEvent:Connect(function(plr, action)
			if action == true then
				gui.Enabled = true
			else
				gui.Enabled = false
			end
		end)

I was thinking something more along the lines of using the same hit function and clones, but a remote event could delete the clone somehow

Something like this could work better.

local Players = game:GetService("Players")
local Part = script.Parent

Part.Touched:Connect(function(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if not Player then return end
	
	if not Player.PlayerGui:FindFirstChild("GUIClonedfromtouchblock") then
		local Clonedgui = script.Parent:FindFirstChildOfClass("ScreenGui"):Clone()
		Clonedgui.Name = "GUIClonedfromtouchblock"
		Clonedgui.Parent = Player.PlayerGui
	end
end)

Part.TouchEnded:Connect(function(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if not Player then return end
	
	local Ui = Player.PlayerGui:FindFirstChild("GUIClonedfromtouchblock")
	Ui:Destroy()
end)
2 Likes

This works perfectly!

I added this line for an exit button on the gui:

local storage = game:GetService("ReplicatedStorage")
local event = storage.exit
event.OnServerEvent:Connect(function(player)
	
	local ui = player.PlayerGui:FindFirstChild("GUIClonedfromtouchblock")
	ui:Destroy()
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.