Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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
Use a LocalScript to handle guis theCJarmy7 1293 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
Wow I really should have gave you the rest of the script, because none of this works with the rest of it xD kittonlover101 201 — 5y
Ad

Answer this question