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

Unwanted script actions?

I made a script (if your from another question your probably a bit familiar with it) and its suppose to display a scope when the right button is clicked and retract it when its up. Of course I want it so that the scope only works when the tool its in is enabled (Being used) For some odd reason it is able to open the scope even when the tool is disabled(Not in use) Please help!!

Tool = script.Parent
Player = game.Players.LocalPlayer
Cam = game.Workspace.CurrentCamera
local Mouse = Player:GetMouse()

Tool.Unequipped:connect(function()
Cam.FieldOfView = 80
Player.CameraMode = "Classic"
if Player.PlayerGui:FindFirstChild("Scope") == nil then
Player.PlayerGui.Scope:Destroy()
end
end) 
Tool.Equipped:connect(function()
Mouse.Button2Down:connect(function()
zoomed = true
Cam.FieldOfView = 20
Player.CameraMode = "LockFirstPerson"
if Player.PlayerGui:FindFirstChild("Scope") == nil then
script.Parent.Scope:Clone().Parent = Player.PlayerGui
end
end)
end)
Tool.Unequipped:connect(function()
	if Player.PlayerGui:FindFirstChild("Scope") == true then
Player.CameraMode = "Classic"
zoomed = false
Cam.FieldOfView = 80
Player.PlayerGui.Scope:Destroy()
	end
end)
Mouse.Button2Up:connect(function()
	Player.CameraMode = "Classic"
	zoomed = false
Cam.FieldOfView = 80
Player.PlayerGui.Scope:Destroy()
end)
Mouse.Button2Up:connect(function()
	if Player.PlayerGui:FindFirstChild("Scope") == true then
	Player.CameraMode = "Classic"
	zoomed = false
Cam.FieldOfView = 80
Player.PlayerGui.Scope:Destroy()
end
end)

I get 0 errors

2 answers

✓ Accepted answer

TAB YOUR CODE CORRECTLY!

Connecting to an event connects to it forever. Thus, from all time after line 14, the right button does something.

You can explicitly disconnect that connection, something like this:

-- in equipped
listen = Mouse.Button2Down:connect(.......

-- in unequipped
listen:disconnect()

Or you can set a boolean whether or not the tool is equipped, and check that. I'll go with the second approach.


You shouldn't have to connect to the same event twice. Merge your code together. Line 9 should be ~= nil rather than == nil. Of course, ~= nil is redundant and can just be dropped. It is wrong to use == true. It is not true. It is a child.


Don't repeat yourself! You have the same code for mouse2up and unequipped. Make one function.


You don't use zoomed anywhere, you might as well drop it. Either that, or use it instead of inspecting the PlayerGui for the Scoped object.

Tool = script.Parent
Player = game.Players.LocalPlayer
Cam = game.Workspace.CurrentCamera
local Mouse = Player:GetMouse()

selected = false -- Whether or not the tool is equipped

function unscope()
	Cam.FieldOfView = 80
	Player.CameraMode = "Classic"
	if Player.PlayerGui:FindFirstChild("Scope") then
		Player.PlayerGui.Scope:Destroy()
	end
end

Tool.Unequipped:connect(function()
	selected = false
	unscope()
end) 

Tool.Equipped:connect(function()
	selected = true
end)

Mouse.Button2Down:connect(function()
	if selected then
		-- Only act if the tool is equipped.
		Cam.FieldOfView = 20
		Player.CameraMode = "LockFirstPerson"
		if not Player.PlayerGui:FindFirstChild("Scope") then
			script.Parent.Scope:Clone().Parent = Player.PlayerGui
		end
	end
end)

Mouse.Button2Up:connect(function()
	unscope()
end)

Well, here's a great suggestion.

Have 2 separate events, Unequipped and Equipped for the tool. When each of them fires, it changes the value of a variable called 'equipped'. Now, when you MouseButton2Down, just check if that value is true!

2 comments

0

but I don't want to use values. Those can be slow and not always instant. Just doing it straight from a script is easier.

0

You don't need values, you need variables.

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