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

How to clone GUI to player if the GUI isn't inside the player already?

What I'm trying to do is when the player touches a part, it checks if the player has the GUI, and if they don't, it clones the GUI into the player. The GUI is supposed to destroy itself 3 seconds after showing the GUI, meaning that you are able to touch the part again. However, this only works once, and touching the part again does nothing. How do I fix this?

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if player.PlayerGui:FindFirstChild("lock") then
			print("lock exists")
		else
			script.lock:Clone().Parent = player.PlayerGui
		end			
	end
end
script.Parent.Touched:connect(onTouched)

2 answers

✓ Accepted answer

there are two reasons i can think of for this

reason A is that you're not actually destroying the gui, but just setting its Enabled property to false (which seems unlikely)

reason B is that you ARE destroying the gui, but only on the client

because of FilteringEnabled, changes made on the client don't replicate to the server, so the gui will still exist on the server

to fix this you'll have to do the destroying inside your touched function, since server scripts only run in the workspace and ServerScriptService

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- findFirstChild is deprecated, use FindFirstChild instead
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player.PlayerGui:FindFirstChild("lock") then
            print("lock exists")
        else
            local clone = script.lock:Clone()
			clone.Parent = player.PlayerGui
			wait(3)
			clone:Destroy()
        end         
    end
end
script.Parent.Touched:Connect(onTouched) -- connect is ALSO deprecated, use Connect instead

1 comment

0

Thank you! I understand now.

Howdy!

This has been something that has been happening to me as well. I don't know if Roblox made some changes but I've discovered that the error was with the clone being set to a parent. I fixed it by making the clone and then giving it the parent in a separate line. It fixed it. Try what I have below, if it doesn't work - comment below.

function onTouched(hit)
    if hit.Parent:findFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            if player.PlayerGui:FindFirstChild("lock") then
            print("lock exists")
        else
            local Clone = script.lock:Clone()
	    Clone.Parent = player.PlayerGui
	    print(Clone.Parent)
        end         
    end
end
script.Parent.Touched:connect(onTouched)

If this helped you out, consider accepting this answer for those sweet, sweet reputation points. If not, comment below and I (or someone else) will help you out.

Be sure to check out the Roblox API Documentation as well for additional reference.

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