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

Mouse.hit position not updating in a while loop?

Hi, I'm trying to make a painting system on a canvas so when the player holds down the click button, parts will spawn very fast at the position of their mouse. Only problem is is that the parts wont update to the mouses position in the while loop. That may seem confusing but here's the code:

LocalScript:

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
local Paint = game.ReplicatedStorage.Paint

Mouse.Button1Down:connect(function()
	local MousePos = Mouse.Hit.p
	local MouseA = Mouse
	if Mouse.Target ~= nil then
		if Mouse.Target.Name == "Canvas" and "ColorPart" then
			Paint:InvokeServer(MousePos, MouseA)
		else
			print("Cant do that")
		end
	end
end)

ServerScript:

local Paint = game.ReplicatedStorage.Paint
ButtonPressed = false

Paint.OnServerInvoke = function(player, MousePos, ButtonPressed)
	local Folder = Instance.new("Folder", game.Workspace)
	ButtonPressed = true
	Folder.Name = "Parts"
	wait()
	if ButtonPressed == true then
		while true do
			wait()
			local Part = Instance.new("Part", game.Workspace.Parts)
			Part.Name = "ColorPart"
			Part.Position = MousePos
			Part.Size = Vector3.new(1.04, 1, 0.05)
			Part.BrickColor = BrickColor.new("Really black")
			Part.Anchored = true
		end
	else
		print("cant do that")
	end
end

If somebody could help me with my problem that would be great! Thanks!

1 answer

Use mouse.Move instead of while true do

Local

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
local Paint = game.ReplicatedStorage.Paint

Mouse.Button1Down:connect(function()
    local MousePos = Mouse.Hit.p
    local MouseA = Mouse
    if Mouse.Target ~= nil then
        if Mouse.Target.Name == "Canvas" and "ColorPart" then
            Paint:InvokeServer(MousePos, MouseA, Mouse)
        else
            print("Cant do that")
        end
    end
end)

Server

local Paint = game.ReplicatedStorage.Paint
ButtonPressed = false

Paint.OnServerInvoke = function(player, MousePos, ButtonPressed, Mouse)
    local Folder = Instance.new("Folder", game.Workspace)
    ButtonPressed = true
    Folder.Name = "Parts"
    wait()
    Mouse.Move:Connect(function()
	    if ButtonPressed == true then
	            wait()
	            local Part = Instance.new("Part", game.Workspace.Parts)
	            Part.Name = "ColorPart"
	            Part.Position = MousePos
	            Part.Size = Vector3.new(1.04, 1, 0.05)
	            Part.BrickColor = BrickColor.new("Really black")
	            Part.Anchored = true
	        end
	    else
	        print("cant do that")
	    end
	end)
end

2 comments

0

Didn't work, this is the error it gave me ServerScriptService.Script:9: attempt to index local 'Mouse' (a nil value)

0

I recommend you do all the server side needed things on the server side and try to keep the mouse out of the server. Do the "checks" on the client-side and also the events of the mouse on the client side.

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