Changing Tool by Mouse Wheel - Help

  1. Everything started when I took a tool swapping by mouse wheel script and rescripted it so it’s what I want. The script works, but there’s one huge problem. It eats your computer RAM so fast and I don’t know why it’s doing it.

  2. How does the script works? When you equip a tool and use mouse scroll it changes the item that you hold from backpack. When you unequip a tool, mouse scrolling doesn’t change the tool that you’re holding in your hands anymore.

  3. Eveything works. Unfortunately I don’t know how to reduce lag. Can you help me in recreating this script?

LocalScript in StarterGui:

    local player = game:GetService("Players").LocalPlayer
    local mouse = player:GetMouse()
    local i = 1

    game:GetService("Workspace")[game:GetService("Players").LocalPlayer.Name].ChildAdded:Connect(function()
    	for _, v in pairs(game:GetService("Players").LocalPlayer.Character:GetChildren()) do 
    		if v:IsA("Tool") then
    			player.CameraMaxZoomDistance = 0.5
    			player.CameraMinZoomDistance = 0.5
    			
    			mouse.WheelForward:Connect(function()
    				if player.Backpack:children()[i-1] and i > 1 then
    					player.Character.Humanoid:UnequipTools()
    					local tool = player.Backpack:children()[i]
    					player.Character.Humanoid:EquipTool(tool)
    					i = i + 1
    				elseif i == 1 then
    					i = #player.Backpack:children()
    					player.Character.Humanoid:UnequipTools()
    					local tool = player.Backpack:children()[i]
    					player.Character.Humanoid:EquipTool(tool)
    				else
    					i = #player.Backpack:children()
    				end
    			end)
    			
    			mouse.WheelBackward:Connect(function()
    				if player.Backpack:children()[i+1] then
    					player.Character.Humanoid:UnequipTools()
    					local tool = player.Backpack:children()[i]
    					player.Character.Humanoid:EquipTool(tool)
    					i = i + 1
    				elseif #player.Backpack:children() < i then
    					i = 1
    					player.Character.Humanoid:UnequipTools()
    					local tool = player.Backpack:children()[i]
    					player.Character.Humanoid:EquipTool(tool)
    				else
    					i = 1
    				end
    			end)
    		end
    	end
    end)

    game:GetService("Workspace")[game:GetService("Players").LocalPlayer.Name].ChildRemoved:Connect(function(Child)
    	if Child:IsA("Tool") then
    		player.CameraMaxZoomDistance = 35
    		player.CameraMinZoomDistance = 0.5
    	end
    end)
1 Like

Use userinputservice for this.

Solution is: use User Input Service do detect whenever mouse scroll has worked. Then if in player character is a tool, change the tool that you have in hands and make a min. camera zoom to 0.5 and max. to 0.5. Whenever tool is deleted from character we change zoom to min. camera zoom 0.5 and max. to how you have in your game.

3 Likes