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

why do i get this error ?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

works in studio mode but not test mode or play

14:09:16.988 - Players.Player1.PlayerGui.ScreenGui.LocalScript:193: attempt to index local 't' (a nil value)

gui.Play.MouseButton1Down:connect (function()
        local r = game.Players.LocalPlayer.Character:FindFirstChild("Head")
        local t = game.ServerScriptService:FindFirstChild("cam2")
        t:Clone().Parent = r
        t:Clone().Parent = r
        t:Clone().Parent = r
        gui.Parent.Parent.Character.Torso.CFrame = CFrame.new(math.random (7370.002,7385.002), 94.889, -8137.3)
        gui.Parent.Parent.Character.Torso.Anchored = false
        gui.Sound:Stop()
        gui.Sound.SoundId = "rbxassetid://142322500"
        gui.Sound:Play()
        gui.Core.Visible = false
        gui.Overlay.Visible = false
        gui.Play.Visible = false
    end)

1 answer

Log in to vote
1
Answered by 9 years ago

Hey there! You didn't give me much information on where this script was running from, however from your error message I can see what the problem is. As this script is running in 'PlayerGui' which is part of the client side objects. (Other client side objects include inside the player and inside the players backpack) When you are trying to find 'cam2', you are looking in SeverScriptService, which is a Server Side object . As client side objects cannot see Server Side objects, your script is returning an error.

There is an easy fix for this, which is to move 'cam2' into game.Lighting which can be accessed from both client side and server side. To prevent errors like this in future, use an if statement, which would work somewhat like this.

local t = game.ServerScriptService:FindFirstChild("cam2",true)
if t then --Without any arguments, this checks if t exists
    t:Clone().Parent = r
    t:Clone().Parent = r
    t:Clone().Parent = r
    --Etc
end

It is good practice to use if when using FindFirstChild, as it will return an error if you haven't found an object.

0
Please accept answer and upvote if this helps! Protoduction 216 — 9y
0
Great answer, and nice explanation. Great job AKAHADES. AmericanStripes 610 — 9y
Ad

Answer this question