I have this problem where my time is just too fast and it runs twice and too slow that it’s not continuous to brush…
In context I am making a brush tool for a game of life that allows me to draw dead or alive cell on it and whenever it hits something it will add a population or kill it
This is my code
local Brush = script.Parent
local Player = game.Players.LocalPlayer
local Character:Model = Player.Character or Player.CharacterAdded:Wait()
local collectionservice = game:GetService("CollectionService")
local Mouse = Player:GetMouse()
local maxdistance = 100
local ChangeColor = game.ReplicatedStorage.RemoteEvent:WaitForChild("ChangeColor")
local PopulationEvent = game.ReplicatedStorage.RemoteEvent:WaitForChild("Population/Generation")
local Toggle = Player.PlayerGui:WaitForChild("Toggle")
local Button = Toggle.Frame.TextButton
local Alive = true
local lastPaintTime = 0
local debounceInterval = 0.035
local holding = false
local color:Color3 = Color3.new(1,1,1)
local AddPopulation:number = 0
Button.Activated:Connect(function()
if Alive then
Alive = false
color = Color3.new(0,0,0)
Button.Text = "Dead"
Button.TextColor3 = Color3.new(1,1,1)
Button.BackgroundColor3 = Color3.new(0,0,0)
print(Alive,color)
else
Alive = true
color = Color3.new(1,1,1)
Button.Text = "Alive"
Button.TextColor3 = Color3.new(0,0,0)
Button.BackgroundColor3 = Color3.new(1,1,1)
end
end)
Brush.Equipped:Connect(function()
Toggle.Enabled = true
Mouse.Button1Down:Connect(function()
holding = true
while holding do
task.wait()
-- POSITIONS
local Head:BasePart = Character:WaitForChild("Head")
local HeadPos = Head.Position
local MousePos = Mouse.Hit.Position
local direction = (MousePos - HeadPos).Unit * 100
--RAYCASTING
local rayCastParams = RaycastParams.new()
rayCastParams.FilterDescendantsInstances = {Character}
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
local result = workspace:Raycast(HeadPos,direction,rayCastParams)
-- VALIDATING
if result then
local hitpart:Part = result.Instance
if collectionservice:HasTag(hitpart,"Box") and hitpart.Color ~= color then
local now = os.clock()
if now - lastPaintTime >= debounceInterval then
lastPaintTime = now
if color == Color3.new(1,1,1) then
AddPopulation += 1
else
AddPopulation -= 1
end
local row, col = hitpart:GetAttribute("Row"), hitpart:GetAttribute("Col")
ChangeColor:FireServer(hitpart,row,col,color)
PopulationEvent:FireServer(AddPopulation)
AddPopulation = 0
end
end
end
end
end)
Mouse.Button1Up:Connect(function()
holding = false
end)
end)
Brush.Unequipped:Connect(function()
Toggle.Enabled = false
end)