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

Not sure to why my GUI only pops up one time?

I've had this problem before, yet I don't remember how to solve it and I can't find my question on how to do this, so could someone please help with my minor issue again?

Info: Server Script inside of a brick

When the brick is touched, it is supposed to give the Gui every 2 seconds (because of debounce) to any player that touches it. Yet it only gives it out the first time and I think because it keeps thinking that it is still inside the PlayerGui.

Maybe I have to make the GUI "false"? Not sure but here


local debounce = true

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not debounce then
			return
		end
		debounce = false
		if not player.PlayerGui:FindFirstChild("ShopGui") then
			print("touched")
			local user = game.Players:GetPlayerFromCharacter(hit.Parent)
			local PG = user:WaitForChild("PlayerGui",1)

			script.Parent.ShopGui:Clone().Parent = PG
			wait(2)
			debounce = true
		else
			print("player already has a shop gui")
			wait(1)
			debounce = true
		end
	end
end)

7 comments

0

Why would it not still be in the PlayerGui? Do you have another script removing it? Perhaps consider replacing it in the else conditional on line 18 if it still exists?

0

For some reason you get the player twice so you could remove the one on line 05. You also don't need to wait for the playergui so user.PlayerGui is fine (You should change user variable to player).

0

Is the first gui that gets added to the player ever destroyed? It should be destroyed on the server since the server had created it.

0

Just for future reference, if you are wondering if you've asked this before, you can go to your reputation page and see every question you've ever asked.

0

I know I can go there but I have asked so many questions before that I dont know for sure which one

0

And yes there is a button to exit and delete the GUI so it is getting deleted out of PlayerGui SO thats why Im not sure why this says the GUI is still valid

0

This is a server script, so it won't see changes made in local script. Destroy the one in the player and replace it on line 18

1 answer

✓ Accepted answer

Your problem is that you're trying to access the Players PlayerGui from a Script. Scripts can't see Local Objects, like a PlayerGui. You're going to have to use a RemoteEvent to contact the player, which will make contact with their PlayerGui.

First you need to put a RemoteEvent named 'event' in ReplicatedStorage

This goes in a LocalScript in StarterCharacterScripts

local me = game.Players.LocalPlayer
local playergui = me:WaitForChild("PlayerGui")
local event = game:GetService('ReplicatedStorage'):WaitForChild("event")

event.OnClientEvent:Connect(function(shopgui)
	if playergui:findFirstChild("ShopGui") then 
		print("I already have that gui")
	else
		shopgui:Clone().Parent = playergui
	end
end)

This goes inside the Script inside your brick.

local debounce = true
local players = game:GetService('Players')
local shopgui = script.Parent:WaitForChild('ShopGui')
local event = game:GetService('ReplicatedStorage'):WaitForChild("event")

script.Parent.Touched:Connect(function(hit)
	local player = players:GetPlayerFromCharacter(hit.Parent)
	local humanoid = player and player.Character:findFirstChild('Humanoid')
	if debounce and humanoid and humanoid.Health > 0 then
		debounce = false
		event:FireClient(player, shopgui)
		wait(2)	
		debounce = true		
	end
end)

6 comments

0

server can definitely see the playergui??

0

:findFirstChild is deprecated, use :FindFirstChild

0

PlayerGui doesn't replicate with FE.

0

I agree with @incapaxian on this. You can go through FE and access the Gui through RemoteEvents.

0

What? That's exactly what I said @MCAndRobloxUnited

0

Wow I didn't expect all that but thanks, Big help! I'll keep that in mind.

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