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

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)

3 comments

0

Line 4 may be the problem, try putting ~= nil as a condition for the two conditions... Also put a print before the if statement and one inside it to see what your script is doing when you touch the block (i.e. is it even detecting if you touch it? Is it working correctly but line 6 is the issue? Etc.)

0

are you using a local script?

0

I tried this script with print and it works correct, just something is wrong in line 6

2 answers

✓ Accepted answer

The problem is that you're using a local script, local scripts don't work in the workspace. I'd suggest using a normal script and putting your GUI inside of the script, and when you touch the brick simply clone the GUI into the player's PlayerGui folder by doing something like this:

local gui = script.gui -- change this to wherever your GUI is
script.Parent.Touched:Connect(function (hit)
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
gui:Clone() -- Clones the GUI
local playerName = hit.Parent.Name
local player = game.Players[playerName]
gui.Parent = player.PlayerGui -- Puts the GUI in the player's PlayerGui Folder
end
end)

I'm not an expert in scripting, and if anyone has a better solution or sees any errors in my answer, feel free to comment! ;)

3 comments

0

Not working mate

0

nvm, it works

0

Thank you

It seems you have your local script inside workspace. The reason why it doesn't work is because local scripts don't work inside workspace. Local scripts will only run if they are a descendant of either a Player's Backpack, Character model, PlayerGui, PlayerScripts or the ReplicatedFirst service.

To fix this you would simply just need to parent the local script somewhere that was stated above.

3 comments

0

Should I use remote events then?

0

No need to use RemoteEvents. Just parent your local script to somewhere I mentioned in my answer. Try putting the local script inside StarterGui and just change script.Parent.Touched to workspace.Part.Touched. Just change 'Part' to the name of the part you want to touch to fire this event.

0

Still not working

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