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

Can someone help me with the playergui error, it seems unavoidable?

I'm trying to make a GUI pop up for people who touch the hitbox of a shop, and I put the localscript for it inside a folder that is in the playergui, which is my script storage. Here is the code

local last = 0

workspace.Shop.CrateShop.CrateShopTouch.Hitbox.Touched:Connect(function(hit)
	if tick() - last <= 1 then
		
	else
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if hum ~= nil then
			local plr = hum.Parent.Name
			local plrC = game.Players:FindFirstChild(plr)
			if plrC.PlayerGui.ScreenGUI:FindFirstChild("CrateShopMenu") == nil then
				local newCrateMenu = game.ReplicatedStorage.Storage.GUIObjects.CrateShopMenu:Clone()
			newCrateMenu.Parent = plrC.PlayerGui.ScreenGUI
			elseif plrC.PlayerGui.ScreenGUI:FindFirstChild("CrateShopMenu") ~= nil then
				
			end
	end
	end
	last = tick()
end)

When the local player touches it, it's doesn't send any errors their way, but it sends out an error for everyone else. If it isn't the local player that is touching it, it sends out something like this "PlayerGui is not a valid member of Player" "Begin" "Players.[LocalPlayerName].PlayerGui.Scripts.ShopTouch Line 11" "End"

where localplayername is, it just shows the name of your player, no matter who touched it.

2 comments

0

Review it carefully. Either this error might actually be unavoidable and you'd need to use a different way, or there was a small error that might have actually screwed it up.

0

is this script a localscript?

1 answer

For what I know, Touch only works in server script. For this, I recommend that you use RemoteEvents with FireClient. On the client event you need to use a Server Script and LocalScript

Your errors: Do not use .Touched in local scripts (for me it never works.), It is not recommended to clone GUIs by a server script, only by the client is the best way in my opinion. (From what I know, You can put GUI's through the server, however it is recommended to put by Client).

Example:

SERVER


local event = game.ReplicatedStorage.RemoteEvent

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
		if player then
			event:FireClient(player)
		end
	elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
		local player = game:GetService("Players"):FindFirstChild(hit.Parent.Parent.Name)
		if player then
			event:FireClient(player)
		end
	end
end

CLIENT

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
	warn("Fired!")
end)

Here is your script fixed:

Part Touch - Server Script(Script) - (put in part)

local last = 0

local event = game:GetService("ReplicatedStorage").RemoteEvent -- Location of event.

function Touch(hit)
	if tick() - last <= 1 then
		
	else
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
			if player then
				event:FireClient(player)
			end
		elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
			local player = game:GetService("Players"):FindFirstChild(hit.Parent.Parent.Name)
			if player then
				event:FireClient(player)
			end
		end
		last = tick();
	end
end

script.Parent.Touched:Connect(Touch)

GetFiredEvent - LocalScript - (put in StarterGui)

repeat wait() until game.Players.LocalPlayer
local player = game.Players.LocalPlayer -- Get localPlayer (ONLY WORKS IN LocalScript)
local playerGui = player.PlayerGui -- Get playerGUI

local GUI = game:GetService("ReplicatedStorage").Storage.GUIObjects.CrateShopMenu -- Location of the object.

local GUI_Holder_Name = "ScreenGUI" -- Put the name of GUI or location.

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function() -- On fire event
	
	playerGui:WaitForChild(tostring(GUI_Holder_Name)) -- Get GUI HOLDER NAME
	
	local Holder = playerGui:FindFirstChild(tostring(GUI_Holder_Name)) -- GET Holder GUI
	
	if Holder then -- IF FIND Holder GUI
		if Holder:FindFirstChild(tostring(GUI.Name)) == nil then -- IF NOT FIND THE CRATE MENU
			GUI:Clone().Parent = Holder -- COPY THE CRATE MENU AND PUT ON HOLDER GUI
		end
	else
		warn("Cannot get HolderGUI")
	end
end)

Hope it helped! :)

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