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

Why is my script not finding a GUI in PlayerGui? Why does it not work in game, but in Studio also?

This script that I made is supposed to tween a Frame in a GUI when you touch a brick, but I keep getting this error:

attempt to index a nil value.

This makes no sense, because the GUI is in "PlayerGui". Also this script works in studio and not in the game itself..

script.parent.Touched:Connect(function(m)
local player = game.Players:GetPlayerFromCharacter(m.Parent);
wait(0.1)
player.PlayerGui:WaitForChild("Teleport").Frame:TweenPosition(UDim2.new(0,0,0,0), "Out", "Linear", 1)
end

Any help would be appreciated.

1 comment

1

Use a LocalScript to handle guis

1 answer

This is because in line 1, it’s script.Parent, not script.parent.

You also tried to modify the GUI in the server, which is a huge no no.

You must use a RemoteEvent and fire it to the client if they touch your block.

-- LocalScript

local plr = game:GetService("Players").LocalPlayer
local remote = Instance.new("RemoteEvent")
remote.Name = "TweenGui"
remote.Parent = game:GetService("ReplicatedStorage")

remote.OnClientEvent:Connect(function()
    plr:WaitForChild("PlayerGui").Teleport.Frame:TweenPosition(
        UDim2.new(), -- your position got cut off so put it here 
        Enum.EasingDirection.Out, -- use this instead of "Out"
        Enum.EasingStyle.Linear, -- here too
        1
)
end)

Now for the server code:

-- ServerScript

local rep = game:GetService"ReplicatedStorage"

local plrs = game:GetService"Players"

script.Parent.Touched:Connect(function(part)
    local plr = plrs:GetPlayerFromCharacter(part.Parent)
    
    if plr then
        rep.TweenGui:FireClient(plr)
    end
end)

1 comment

0

Wow I really should have gave you the rest of the script, because none of this works with the rest of it xD

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