I have a placeable bush tool that I made this morning, but when I move the mouse around, the preview object keeps flashing.
I made it so the preview wouldn’t display if it was touching an anchored part, but would display if it was touching an unanchored part.
https://gyazo.com/4b3229ca0b3257f583b409044a6542df
What is causing this?
I also received a client error during a playtest that said “Attempt to index nil with ‘Anchored’”. I think it’s from the script checking if Mouse.Target is unanchored.
Here is the code:
local P = game:GetService("Players")
local plr = P.LocalPlayer
local Mouse = plr:GetMouse()
local rs = game:GetService("RunService")
local RS = game.ReplicatedStorage
local uis = game:GetService("UserInputService")
local tool = script.Parent
local event = tool:WaitForChild("RemoteEvent")
local equipped = false
local connection = nil
local inputConnection = nil
local preview = nil
local rotateIncrement = 10
local maxBushes = 5
local bushesPlaced = 0
tool.Equipped:Connect(function()
equipped = true
preview = RS.BushCamoAddon:Clone()
preview.Anchored = true
preview.Parent = workspace
Mouse.TargetFilter = preview
connection = rs.RenderStepped:Connect(function()
if equipped == true and preview ~= nil and Mouse.Target.Anchored == false then
preview.Transparency = 0.1
preview.Position = Mouse.Hit.p
else
preview.Transparency = 1
end
end)
inputConnection = uis.InputBegan:Connect(function(key,gpe)
if not gpe then
if key.KeyCode == Enum.KeyCode.R then
preview.CFrame = preview.CFrame * CFrame.Angles(0,math.rad(rotateIncrement),0)
elseif key.KeyCode == Enum.KeyCode.T then
preview.CFrame = preview.CFrame * CFrame.Angles(0,0,math.rad(rotateIncrement))
elseif key.KeyCode == Enum.KeyCode.L then
event:FireServer("Reset")
bushesPlaced = 0
end
end
end)
end)
tool.Unequipped:Connect(function()
equipped = false
preview:Destroy()
preview = nil
Mouse.TargetFilter = nil
connection:Disconnect()
inputConnection:Disconnect()
end)
tool.Activated:Connect(function()
if Mouse.Target.Anchored == false and (bushesPlaced < maxBushes) then
event:FireServer("Place", preview.CFrame, Mouse.Target)
bushesPlaced = bushesPlaced + 1
end
end)