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

why do i get this error ?

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

✓ Accepted answer

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.

2 comments

0

Please accept answer and upvote if this helps!

0

Great answer, and nice explanation. Great job AKAHADES.

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