Hello I am making a game where you customize terrain but the painting only works at specific times
The code I am using for this in StarterPlayerScripts
local movepart = game.Workspace:WaitForChild("HoverPart")
local mouse = game.Players.LocalPlayer:GetMouse()
local ismouseheld = false
function Mousedown()
ismouseheld = true
end
function mouseup()
ismouseheld = false
end
mouse.Button1Up:Connect(mouseup)
mouse.Button1Down:Connect(Mousedown)
while true do
if ismouseheld == true then
if game:GetService("ReplicatedStorage"):WaitForChild("Player_Values"):WaitForChild("IsPlayerBuilding").Value == true then
local mousex = mouse.Hit.Position.X
local mousey = mouse.Hit.Position.Y
local mousez = mouse.Hit.Position.Z
workspace.Terrain:FillBall(Vector3.new(mousex, mousey, mousez), 2, Enum.Material.Ground)
wait(0.2)
end
if game:GetService("ReplicatedStorage"):WaitForChild("Player_Values"):WaitForChild("IsPlayerPaintBrush").Value == true then
local mousePos = mouse.Hit.Position
local regionMin = mousePos - Vector3.new(2.5, 2.5, 2.5)
local regionMax = mousePos + Vector3.new(2.5, 2.5, 2.5)
local region = Region3.new(regionMin, regionMax)
workspace.Terrain:ReplaceMaterial(region, 4, Enum.Material.Ground, Enum.Material.Ice)
wait(0.1)
end
else
print("Mouse Aint Down")
end
wait(0.00001)
end
local movepart = game.Workspace:WaitForChild("HoverPart")
local mouse = game.Players.LocalPlayer:GetMouse()
local isMouseHeld = false
local replicatedStorage = game:GetService("ReplicatedStorage")
local playerValues = replicatedStorage:WaitForChild("Player_Values")
local isPlayerBuilding = playerValues:WaitForChild("IsPlayerBuilding")
local isPlayerPaintBrush = playerValues:WaitForChild("IsPlayerPaintBrush")
local Wip = playerValues:WaitForChild("WIBWOPW")
local whatmutiral = Enum.Material.Ground
-- Mousedown and Mouseup functions
function Mousedown()
isMouseHeld = true
end
function Mouseup()
isMouseHeld = false
end
mouse.Button1Up:Connect(Mouseup)
mouse.Button1Down:Connect(Mousedown)
-- Time delay for the building action (in seconds)
local actionDelay = 1
local lastActionTime = 0
game:GetService("RunService").Heartbeat:Connect(function()
if isMouseHeld then
local currentTime = tick()
-- Building logic (with delay)
if isPlayerBuilding.Value and (currentTime - lastActionTime >= actionDelay) then
local mousePos = mouse.Hit.Position
workspace.Terrain:FillBall(mousePos, 5, Enum.Material.Ground)
-- Update the last action time after the building action
lastActionTime = currentTime
end
-- Painting logic (without delay)
if isPlayerPaintBrush.Value then
local mousePos = mouse.Hit.Position
local regionMin = mousePos - Vector3.new(5, 5, 5)
local regionMax = mousePos + Vector3.new(5, 5, 5)
local region = Region3.new(regionMin, regionMax)
workspace.Terrain:ReplaceMaterial(region, 4, whatmutiral, Enum.Material.Ice)
end
else
print("Mouse is not down.")
end
end)