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

Why my script to appear GUI when you click block doesn't works?

Hello! I'm trying to make GUI appear when I touch block. I did this script below but it doesn't works, also I don't have any errors, any ideas what should I change? (script is in local script)

local Player = game.Players.LocalPlayer

local function change(hit)
    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then

        Player.PlayerGui.UniformGui.Frame.Visible=true

    end
end


script.Parent.Touched:Connect(change)

2 answers

From the documentation for LocalScripts:

A LocalScript will only run Lua code if it is a descendant of one of the following objects: • A Player’s Backpack, such as a child of a Tool • A Player’s character model • A Player’s PlayerGui • A Player’s PlayerScripts. • The ReplicatedFirst service

So if the script.Parent is somewhere in workspace (without being inside the player's character model), the script ends up not running. If this was a Script instead, it would not work either as Players.LocalPlayer is only available on the client.

The script can be rewritten to get the part in some other way, then moved to somewhere in one of the listed locations. A good way to get the part(s) is tagging the parts which should make the GUI appear and getting them with CollectionService, like in the example here.

I am assuming that the script, you have mentioned is Local Script. I recommend you to use Server script instead because Local Scripts only work in StarterGui, StarterPlayerScripts and StarterCharacterScripts.

When you make this a script, just remove:

local Player = game.Players.LocalPlayer

Next, create a Remote Event in ReplicatedStorage of any name, for now, eg : RemoteEvent.

Now, instead of:

Player.PlayerGui.UniformGui.Frame.Visible=true

Do this,

-- We need a player parameter, for that you can simply do:
local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- It will get the player

game.ReplicatedStorage.RemoteEvent:FireClient(plr) -- This will fire the remote event to the player

This is how your server script might look inside the part:

local function change(hit)
    if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then

        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	game.ReplicatedStorage.RemoteEvent:FireClient(plr)

    end
end


script.Parent.Touched:Connect(change)

Now, lets set up the Local Script:

-- Keep your Local Script in StarterGui

local Player = game.Players.LocalPlayer -- This will get the player
local YourTime = 3 -- Set it to your own value, it's 3, it means for every touch in 3 seconds, the frame visibility will become false

--Time for adding a little debounce
local d = false -- Creating a boolean value

if not d then --Checks if d is false
	d = true --Sets the value to true

	game.ReplicatedStorage.RemoteEvents.OnClentEvent:Connect(function()
		Player.PlayerGui.UniformGui.Frame.Visible = true
	end)

	wait(YourTime)
	d = false -- Sets the value to false again
end

Lemme know if it helps!

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