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 4 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:

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

Log in to vote
1
Answered by 4 years ago

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
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 — 4y
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 — 4y
Ad

Answer this question