Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Mouse.hit position not updating in a while loop?

Asked by 5 years ago

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:

01Player = game.Players.LocalPlayer
02Mouse = Player:GetMouse()
03local Paint = game.ReplicatedStorage.Paint
04 
05Mouse.Button1Down:connect(function()
06    local MousePos = Mouse.Hit.p
07    local MouseA = Mouse
08    if Mouse.Target ~= nil then
09        if Mouse.Target.Name == "Canvas" and "ColorPart" then
10            Paint:InvokeServer(MousePos, MouseA)
11        else
12            print("Cant do that")
13        end
14    end
15end)

ServerScript:

01local Paint = game.ReplicatedStorage.Paint
02ButtonPressed = false
03 
04Paint.OnServerInvoke = function(player, MousePos, ButtonPressed)
05    local Folder = Instance.new("Folder", game.Workspace)
06    ButtonPressed = true
07    Folder.Name = "Parts"
08    wait()
09    if ButtonPressed == true then
10        while true do
11            wait()
12            local Part = Instance.new("Part", game.Workspace.Parts)
13            Part.Name = "ColorPart"
14            Part.Position = MousePos
15            Part.Size = Vector3.new(1.04, 1, 0.05)
View all 22 lines...

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

1 answer

Log in to vote
1
Answered by 5 years ago

Use mouse.Move instead of while true do

Local

01Player = game.Players.LocalPlayer
02Mouse = Player:GetMouse()
03local Paint = game.ReplicatedStorage.Paint
04 
05Mouse.Button1Down:connect(function()
06    local MousePos = Mouse.Hit.p
07    local MouseA = Mouse
08    if Mouse.Target ~= nil then
09        if Mouse.Target.Name == "Canvas" and "ColorPart" then
10            Paint:InvokeServer(MousePos, MouseA, Mouse)
11        else
12            print("Cant do that")
13        end
14    end
15end)

Server

01local Paint = game.ReplicatedStorage.Paint
02ButtonPressed = false
03 
04Paint.OnServerInvoke = function(player, MousePos, ButtonPressed, Mouse)
05    local Folder = Instance.new("Folder", game.Workspace)
06    ButtonPressed = true
07    Folder.Name = "Parts"
08    wait()
09    Mouse.Move:Connect(function()
10        if ButtonPressed == true then
11                wait()
12                local Part = Instance.new("Part", game.Workspace.Parts)
13                Part.Name = "ColorPart"
14                Part.Position = MousePos
15                Part.Size = Vector3.new(1.04, 1, 0.05)
View all 23 lines...
0
Didn't work, this is the error it gave me ServerScriptService.Script:9: attempt to index local 'Mouse' (a nil value) Master_Aaron 59 — 5y
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. BradNewTypical 232 — 5y
Ad

Answer this question