How would I make a mouse click enable a LocalScript?

So I have two cameras one is The regular behind the shoulder Roblox one and the other is one I scripted to be down the iron sights of a gun and I’m trying to enable the second script when pressing MouseButton2 help is appreciated

You should not be enabling or disabling scripts

Why is that? I’m not the best at scripting and it seemed like the best way

Probably be best to just toggle between view points … However,
what you’re asking for you’ll want to for sure toggle right.

local UserInputService = game:GetService("UserInputService")
local aimScript = script.Parent:WaitForChild("Script")

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if aimScript then aimScript.Enabled = true end
	end
end)

UserInputService.InputEnded:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if aimScript then aimScript.Enabled = false end
	end
end)
2 Likes

FYI, Instance:WaitForChild is guaranteed to return the target child. If the target child does not exist, the caller thread will be yielded until it does. You can simplify the if-statement by removing the aimScript clause

1 Like

Ya, I don’t like using FindFirstChild() unless I’m looking for ~=nil or not. But I do like stalling a script to give everything else time to load… I’ll just make sure it’s there. I like the error, telling me to do it right. :smile:

Let’s say you had a part on that gun named sight … and that was where you wanted the view.

Toggle
This was a good prototype, but got this working nicely with a tween zoom too.
Had to go to PMs as it took a bit of posting ..
1 Like

This script isn’t working for some reason I have it as a LocalScript in StarterPlayerScripts do you know any solutions?

I’m being kind of loose with this reference …
local aimScript = script.Parent:WaitForChild(“Script”)
Make sure that is set up right for you …
Still have this is the studio toggling fine.

The other script would use a transparent part welded or an Attachment.
Attachments work well for that kind of stuff and even have an offset for fine-tuning.
As seen in the race demo, how the car can swap to 4 different views.

1 Like

Its still not working unfortunately I probably should have mentioned it in the post but the model is a custom StarterCharacter would that mess with the script at all?

Stating to sound like it… Any errors any information? Are scripts there?
That is just a simple toggle.

There’s a infinite yield for the gun model and the sight but I don’t see anything else the project output is very messy with other stuff

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.