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

How to ensure script will only try to work if mouse.target ~= nil? [read desc]

This script keeps working in studio, but not in game. I check in game each time and im getting errors about mouse.target being nil but i keep trying to add fail safes to make sure that the thing only works when mouse.target isnt nil. The script is supposed to display a billboardgui on a part named "Detect" when it is mouse.target.

script:

i = false
game.Players.PlayerAdded:connect(function(plr)
	lead = Instance.new('Model', plr)
	lead.Name = "leaderstats"
	
	log = Instance.new('NumberValue', lead)
	log.Name = "Log Count"
	
	mouse = plr:GetMouse()
	plr.CharacterAdded:connect(function(chr)
		repeat
			wait()
			if plr:GetMouse().target ~= nil then
				if plr:GetMouse().target.Name == "Detect" then
					if plr:GetMouse().target.Value.Value == true then
						if mouse.target:FindFirstChild('CollectGui') then
						--
						else
							magnitude = (plr:GetMouse().target.Position - chr.Torso.Position).magnitude
							print(magnitude)
							if magnitude < (Vector3.new(10, 10, 10) - Vector3.new(0, 0, 0)).magnitude then
								local gui = game.Lighting.CollectGui:Clone()
								gui.Parent = plr:GetMouse().target
							end
						end
						wait()
					end
				end
			end
		until i == true
	end)
end)

place: https://www.roblox.com/games/557858400/MrDefaultMans-Place-Number-93

1 answer

You can't get the player's mouse in a regular script, or on the server. You may need to convert this to a Local Script.

It's important to note that Local Scripts don't run in workspace or in ServerScriptService. You'll have to place it somewhere where it will run locally on the client.

1 comment

0

thanks ill try to see what i can do (when im not too tired to script anymore)

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