Why is my tycoon mouse script not working?

hey,
I’m currently working on a tycoon/simulator game and somehow is my script not working

The script :

local mouse = game.Players.LocalPlayer:GetMouse()
local mouseposition = mouse.Hit.Position
local player = game.Players.LocalPlayer
local Chr = player.Character
local mon = player:WaitForChild(“AISDHAOSDHASI”):FindFirstChild(“2940u10iashöodwe”).Value
local hold = false

mouse.Button1Down:Connect(function()
hold = true
local tycoon = workspace.Tycoons:FindFirstChild(player.UserId)

while hold do --while button is hold 
	if tycoon then --checks for the tycoon
		if tycoon:FindFirstChild("field1") then 
			local field = tycoon:FindFirstChild("field1") 
			local fold = field:FindFirstChild("Folder")
			local wheats = fold:GetChildren()
			
			if Chr:FindFirstChild("starter") then -- checks if player has starter tool equipped
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 3 then 
							i.Transparency = 1 
							mon += 1
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 
				
			elseif Chr:FindFirstChild("firstupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 5 then 
							i.Transparency = 1 
							mon += 1
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 
				
		elseif Chr:FindFirstChild("secondupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 7 then 
							i.Transparency = 1 
							mon += 1
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 
				
			elseif Chr:FindFirstChild("thirdupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 9 then 
							i.Transparency = 1 
							mon += 1
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 
				
			elseif Chr:FindFirstChild("fourthupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 11 then 
							i.Transparency = 1 
							mon += 1
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 
		   end --ends chr:findfirstchild
			
		elseif tycoon:FindFirstChild("field2") then 
			local field = tycoon:FindFirstChild("field2") 
			local fold = field:FindFirstChild("Folder")
			local wheats = fold:GetChildren()

			if Chr:FindFirstChild("starter") then -- checks if player has starter tool equipped
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 3 then 
							i.Transparency = 1 
							mon += 3
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 

			elseif Chr:FindFirstChild("firstupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 5 then 
							i.Transparency = 1 
							mon += 3
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 

			elseif Chr:FindFirstChild("secondupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 7 then 
							i.Transparency = 1 
							mon += 3
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 

			elseif Chr:FindFirstChild("thirdupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 9 then 
							i.Transparency = 1 
							mon += 3
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 

			elseif Chr:FindFirstChild("fourthupdate") then 
				for v, i in pairs(wheats) do 
					if i and i:IsA("MeshPart") then
						local distance = (i.Position - mouseposition).Magnitude -- gets distance 
						if distance < 11 then 
							i.Transparency = 1 
							mon += 3
							wait(60)
							i.Transparency = 0 
						end --ends if distance
					end--ends if i 
				end --ends for v,i 
				
			else 
				
			end --ends Chr:FindFirstChild
			
		else 
			
		end --ends if field2
		
	else 
		
	end --ends if tycoon
	wait()
end--ends while hold

end) --ends function

1 Like
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local mouseposition = mouse.Hit.Position
local mon = player:WaitForChild("AISDHAOSDHASI"):FindFirstChild("2940u10iashöodwe").Value --change the names of these
local hold = false

I made some fixes to the bit above the callback connection, I’ve also added a comment where you should change the names of 2 instances.

If you could provide a little more info/context about what specifically isn’t working that’d be helpful.

I’m sorry I just realized afterward the long script isn’t needed and so i made a shorter version of the script

The script is supposed to makeMeshParts invisible and give the player money
The script actually worked but suddenly it stopped working.
I think I accidentally deleted something or did something wrong

The script:

local mouse = game.Players.LocalPlayer:GetMouse()
local mouseposition = mouse.Hit.Position
local player = game.Players.LocalPlayer
local Chr = player.Character
local mon = player:WaitForChild(“DataStore”):FindFirstChild(“money”).Value
local hold = false

script.Parent.Equipped:Connect(function()
mouse.Button1Down:Connect(function()
hold = true
local tycoon = workspace.Tycoons:FindFirstChild(player.UserId)

while hold do --while button is hold 
	if tycoon then --checks for the tycoon
		if tycoon:FindFirstChild("field1") then 
			local field = tycoon:FindFirstChild("field1") 
			local fold = field:FindFirstChild("Folder")
			local wheats = fold:GetChildren()
			
			for v, i in pairs(wheats) do 
				if i and i:IsA("MeshPart") then
					local distance = (i.Position - mouseposition).Magnitude -- gets distance 
					if distance < 3 then 
						i.Transparency = 1 
						mon += 1
					end --ends if distance
				end--ends if i 
			end --ends for v,i 
   end--ends if tycoon:FindFirstChild
end--ends if tycoon

wait()
end --ends while hold
end)
end)

This code seems very messy. Can’t you use the Touched event, then use the TouchEnded event, instead of making a while loop?

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local mouseposition = mouse.Hit.Position
local mon = player:WaitForChild("DataStore"):FindFirstChild("money")
local hold = false
local tool = script.Parent
local tycoons = workspace:WaitForChild("Tycoons")
local tycoon = tycoons:FindFirstChild(player.UserId)
local mouseUpEvent
local mouseDownEvent

tool.Equipped:Connect(function()
	mouseUpEvent = mouse.Button1Up:Connect(function()
		hold = false
	end)
	
	mouseDownEvent = mouse.Button1Down:Connect(function()
		hold = true
		while hold do
			task.wait()
			if tycoon then --checks for the tycoon
				if tycoon:FindFirstChild("field1") then 
					local field = tycoon:FindFirstChild("field1") 
					local folder = field:FindFirstChild("Folder")
					local wheats = folder:GetChildren()

					for i, v in pairs(wheats) do 
						if v:IsA("MeshPart") then
							local distance = (v.Position - mouseposition).Magnitude
							if distance <= 3 then 
								v.Transparency = 1
								mon += 1
							end
						end
					end
				end
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	mouseUpEvent:Disconnect()
	mouseDownEvent:Disconnect()
end)

There is no “Touched” event, I feel like you’re rushing a bit with some of these replies.

I made quite a few changes to the script so instead of explaining all of them if you need any assistance/explanation for anything don’t hesitate to ask, I and I’m sure many others on the forum would be happy to help.

Yes, but how does the loop know the thing is being held? It will stay true until it Is set to false, which unfortunately never happens, so it will keep running.

mouseUpEvent = mouse.Button1Up:Connect(function()
		hold = false
	end)
	
	mouseDownEvent = mouse.Button1Down:Connect(function()
		hold = true
		while hold do

Ignore the indentation.

That would basically be what they need. They need to verify that the person is actually holding the button.