Mouse Hit Problem with Custom Placement Script

Hey guys, I’ve been working on a custom building system for my game and I ran into a problem. The problem is that when I press Q for the wall to appear, the wall goes all crazy for like 3 seconds and then goes perfect. This script below is a part of the script and I’ll show a video too.

local function spawnWallPrefab(prefab)
	if currentWallPreview then
		currentWallPreview:Destroy()
	end
	currentWallPrefab = prefab
	currentWallPreview = currentWallPrefab:Clone()
	currentWallPreview.Parent = workspace:WaitForChild("WallsPreview")
	currentWallPreview.Transparency = 0.5 -- Set transparency for the preview
	currentWallPreview.CanCollide = false
	currentWallPreview.BrickColor = BrickColor.new("Baby blue")

	-- Set the initial position and orientation of the wall preview
	currentWallPreview.Position = mouse.Hit.Position + Vector3.new(0, 5, 0) -- Adjust Y offset as needed
	currentWallPreview.CFrame = CFrame.new(currentWallPreview.Position)
end

-- Function to update the wall preview position
local function updateWallPreview()
	if currentWallPreview then
		currentWallPreview.Position = mouse.Hit.Position + wallPreviewOffset
	end
end

-- Function to rotate the wall preview
local function rotateWallPreview()
	if currentWallPreview then
		rotationAngle = rotationAngle + 90
		currentWallPreview.CFrame = CFrame.Angles(0, math.rad(rotationAngle), 0) * CFrame.new(currentWallPreview.Position)
		print(rotationAngle)
	end
end

-- Function to handle MouseButton1 click
local TweenService = game:GetService("TweenService")

-- Function to handle MouseButton1 click
local function onMouseButton1Click()
	if buildMode and currentWallPreview then
		if resources.Wood.Value >= 10 then
			currentWallPreview.Position = mouse.Hit.Position + wallOffset -- Adjust the position with the offset
			resources.Wood.Value = resources.Wood.Value - 10
			buildMode = false

			-- Send request to server to place wall
			PlaceWallEvent:FireServer(currentWallPreview.Position, rotationAngle)
			currentWallPreview:Destroy()
			currentWallPreview = nil
		elseif resources.Wood.Value < 10 then
			print("Not enough wood!")
			if currentWallPreview then
				local originalColor = currentWallPreview.BrickColor
				currentWallPreview.BrickColor = BrickColor.new("Bright red") -- Change preview color to red
				wait(1) -- Wait for 1 second
				currentWallPreview.BrickColor = originalColor
			end	
		end
	end
end

set Mouse.TargetFilter to the wall

1 Like
local function spawnWallPrefab(prefab)
    if currentWallPreview then
        currentWallPreview:Destroy()
    end
    currentWallPrefab = prefab
    currentWallPreview = currentWallPrefab:Clone()
    currentWallPreview.Parent = workspace:WaitForChild("WallsPreview")
    currentWallPreview.Transparency = 0.5 -- Set transparency for the preview
    currentWallPreview.CanCollide = false
    currentWallPreview.BrickColor = BrickColor.new("Baby blue")

    -- Set the initial position and orientation of the wall preview
    currentWallPreview.Position = mouse.Hit.Position + Vector3.new(0, 5, 0) -- Adjust Y offset as needed
    currentWallPreview.CFrame = CFrame.new(currentWallPreview.Position)
end

-- Function to handle MouseButton1 click
local function onMouseButton1Click()
    if buildMode and currentWallPreview then
        if resources.Wood.Value >= 10 then
            currentWallPreview.Position = mouse.Hit.Position + wallOffset -- Adjust the position with the offset
            resources.Wood.Value = resources.Wood.Value - 10
            buildMode = false

            -- Send request to server to place wall
            PlaceWallEvent:FireServer(currentWallPreview.Position, rotationAngle)
            currentWallPreview:Destroy()
            currentWallPreview = nil
        elseif resources.Wood.Value < 10 then
            print("Not enough wood!")
            if currentWallPreview then
                local originalColor = currentWallPreview.BrickColor
                currentWallPreview.BrickColor = BrickColor.new("Bright red") -- Change preview color to red
                wait(1) -- Wait for 1 second
                currentWallPreview.BrickColor = originalColor
            end	
        end
    end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.