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 camera script not working?

I've decided to take a break from my next big game (since there were like a thousand errors that you guys didn't help me on.) so now i'm working on this Gui that allows you to change your camera mode. Here is how it looks like: StarterGui>CameraModeGui>ScrollingFrame>ShoulderCam>LocalScript OldRobloxCam>LocalScript (the newline is for the two objects in the same parent) so here is the first localscript:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local hit = mouse.Hit
function Onhit(hit)
workspace.CurrentCamera.CameraSubject = plr.Character["Right arm"]
end

the second one is:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local hit = mouse.Hit
function Onhit(hit)
workspace.CurrentCamera.CameraSubject = plr.Character.Head
end

ok, heres the problem; when I tested it out in solo,(doesn't matter) I clicked on the first one and **nothing happened.**I clicked the second one but it had the same problem. I looked in the output and got:

19:24:19.065 - Plugin attempting to show toolbar that does not exist - 96 19:24:19.105 - Unable to load plugin icon. Image may have an invalid or unknown format. Cutscene Editor Loaded Jojomen56's Utility Plugin successfully loaded.

Mind the errors, if i fix it, you wont be able to use studio tools, sorry if it bugs you! 19:24:25.469 - Plugin_144711773.ArmorMaker.Script:41: attempt to index global 'gui' (a nil value) 19:24:25.470 - Stack Begin 19:24:25.470 - Script 'Plugin_144711773.ArmorMaker.Script', Line 41 19:24:25.471 - Stack End 19:24:25.471 - Disconnected event because of exception

Mind the errors, if i fix it, you wont be able to use studio tools, sorry if it bugs you! 19:24:25.472 - Plugin_144711773.ArmorMaker.Script:25: attempt to index global 'gui' (a nil value) 19:24:25.472 - Stack Begin 19:24:25.473 - Script 'Plugin_144711773.ArmorMaker.Script', Line 25 - global Off 19:24:25.473 - Script 'Plugin_144711773.ArmorMaker.Script', Line 11 19:24:25.473 - Stack End 19:24:25.474 - Disconnected event because of exception 19:24:25.474 - Unable to create an Instance of type "StockSound" 19:24:25.475 - Script '[string "--RoFinder Runtime Source..."]', Line 2442 19:24:25.475 - Stack End 19:30:22.364 - Auto-Saving...

Since that has nothing to do with my problem, I don't know what the problem is. (Every script I make acts like it's not there.) Could someone please point out to me the problem?

Thanks for reading!

3 comments

0

Are these the full scripts? I don't see a line that calls your functions when the button gets clicked.

0

you mean the :connect() thingy, I do not know how to do that. And yes, it is a full script.

0

You need the ":connect() thingy," because it is the method that triggers the script. Without it, the function you want to run will not run.

1 answer

A connection line is a line that contains the 'connect' method to call your function, whether or not it is anonymous.

In this example, when the Parent of the script is touched, it will trigger the 'example' function.

function example(argument)
-- Content
end

script.Parent.Touched:connect(example)

The formula to call a function with the 'connect' method is this:

Function [Object].[Event]:connect([FunctionName])

Anonymous Function [Object].[Event]:connect(function ([Argument])

end)

Just in case if you're asking, an anonymous function is simply a function without a name.

In the aforementioned example, this is how an anonymous function is laid out:

script.Parent.Touched:connect(function (argument)
-- Content
end

Going back to your script, here's a possible solution you may consider using to fix your script:

repeat wait(.5) until game.Players.LocalPlayer ~=nil

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local hit = mouse.Hit

function Onhit(hit)
	if plr.Character:FindFirstChild("Right arm") then		-- I added this statement just in case if there is no right arm
		workspace.CurrentCamera.CameraSubject = plr.Character:FindFirstChild("Right arm")
	else
		return nil
	end
end

function Onhit2(hit)
	if plr.Character:FindFirstChild("Head") then		-- I added this statement just in case if the head is removed; this will not return any errors
		workspace.CurrentCamera.CameraSubject = plr.Character:FindFirstChild("Head")
	else
		return nil
	end
end

script.Parent.TextButton.MouseButton1Down:connect(OnHit)
script.Parent.TextButton2.MouseButton1Down:connect(OnHit2)		-- Adjust the location and/or identity of the button(s) to your desire

The reason why I put both of the functions in one script is because the script is more efficient space-wise and both of the functions can work in one script.

4 comments

0

Thanks! I'll just copy your statements and edit them each time i create a new camera position!

0

Its still not working. Everytime I click it prints no output and it does nothing.

0

Try my edit again. I added "repeat wait(.5) until game.Players.LocalPlayer ~= nil", because sometimes the script ran before the player loads, so the script breaks.

0

Also, make sure that the script is a local script, because it can access to the local player, as opposed to a server script.

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