Okay before you go ahead and ask about the title, its a completely different context.
I am currently making a building a deleting system in my circuit game so you can place down circuits and delete them at any time you want.
The OnHover function is called by the mouse.Move event and the OnHit function is called by the mouse.Button1Down event.
Now, when the player is hovering over a certain part which is in the objectsFolder then it should add a selectionBox onto it, now when the player clicks that part, it deletes it. You get the point.
However, the “matching system” as you could call doesnt work. In the Module script you can see: mouse.Target == v or mouse.Target.Parent == v which is used and is supposed to check if the two parts are the same.
This doesnt work at all, i have also tried to use the Name as a way to match them together, in the OnHit function. But it just errors and doesnt work as usual.
If you know a fix to this i would be greatly appreciated
Module Script:
local objectsFolder = game.ReplicatedStorage.Objects
removerModule.OnHover = function(mouse)
for _, v in pairs(objectsFolder:GetDescendants()) do
if v == nil or v.Parent == nil then
return
end
-- Check if the object or its parent matches the mouse target
if mouse.Target == v or mouse.Target.Parent == v then
warn("found match")
-- If a SelectionBox doesn't already exist, create one
if not v:FindFirstChild("SelectionBox") then
local selectionBox = Instance.new("SelectionBox")
selectionBox.Adornee = v
selectionBox.LineThickness = 0.5
selectionBox.Color3 = Color3.new(1, 0, 0)
selectionBox.Parent = v
end
else
-- If the object has a SelectionBox and it is not the target, remove it
if v:FindFirstChild("SelectionBox") then
v.SelectionBox:Destroy()
end
end
end
end
removerModule.OnHit = function (mouse)
for _, v in pairs(objectsFolder:GetDescendants()) do
if v == nil or v.Parent == nil then
return
end
if mouse.Target.Name == v.Name or mouse.Target.Parent.Name == v.Name then
v:Destroy()
end
end
end
return removerModule
One reason your script is not working might be becasue the objectsFolder you are looping over is located in replicated storage which means the objects there dont interact with the actual game.
What i would recommend instead is creating a new tag and then instead of looping over a folder you would just check if mouse.Target has that tag.
Another reason this script might not work is if your module is server side (required by a server script). The player mouse cannot be replicated over to the server, so if this is the case what you should do is instead of sending the entire mouse you should only send what you need which in this case that would be the mouse.Target
Here is your script with these changes:
local CollectionService = game:GetService("CollectionService")
removerModule.OnHover = function(target)
if target and CollectionService:HasTag(target, "Interactable") then
-- selection box logic
end
end
removerModule.OnHit = function(target)
if target and CollectionService:HasTag(target, "Interactable") then
target:Destroy()
end
end
For more information about CollectionService you can check out this page:
thank you, my placing system is now finished and i have copied it to my game i am working on.
However i seem to be encountering an issue with the :Clone() when i clone one of my components in my circuit game it just breaks.
The server script that is supposed to manage the power of the socket and plugs just stops. It doesnt power and does nothing? what happened i am confused.
Heres the script where it clones the model or part:
QUICK NOTE: when i put the components back into workspace, it works fine. So something seems to be going wrong with the cloning, but nothing is lost or anything so i am unsure of the problem.
sendToServerEvent.OnServerEvent:Connect(function (player, object)
local clonedObject = object:Clone()
clonedObject.Parent = workspace
if clonedObject:IsA("BasePart") then
local characterPosition = player.Character.HumanoidRootPart.Position
local lookVector = player.Character.HumanoidRootPart.CFrame.LookVector
local distanceInFront = 5 -- Set this to whatever distance you need
clonedObject.Position = characterPosition + (lookVector * distanceInFront)
elseif clonedObject:IsA("Model") then
local characterPosition = player.Character.HumanoidRootPart.Position
local lookVector = player.Character.HumanoidRootPart.CFrame.LookVector
local distanceInFront = 5 -- Set this to whatever distance you need
clonedObject:PivotTo(CFrame.new(characterPosition + (lookVector * distanceInFront)))
end
end)
heres the script that manages the power and plugging in the components:
local socketsFolder = script.Parent
local sockets = collectionService:GetTagged("Socket")
local cooldown = 1
local function createWeld(part1, part2)
local newweld = Instance.new("WeldConstraint")
newweld.Parent = part1
newweld.Part0 = part1
newweld.Part1 = part2
end
local function destroyWelds(part)
if part:FindFirstChildOfClass("WeldConstraint") then
part.WeldConstraint:Destroy()
end
end
local function updatePower(plugPower, socketPower)
if socketPower.Value == true then
plugPower.Value = true
else
plugPower.Value = false
end
end
for i, socket in pairs(sockets) do -- goes throught all of the sockets that have the "Socket" tag.
local hitBox = socket:FindFirstChild("HitBox")
local proximityPrompt = hitBox:FindFirstChild("ProximityPrompt")
local socketPower = socket.Configurations:FindFirstChild("Powered")
local powerSwitch = socket.Configurations:FindFirstChild("SwitchPower") -- determines which way the power will go, plug to socket if set to true
local debounce = false
local plug -- nil value for plug
hitBox.Touched:Connect(function (part)
print("touched")
local connected = part:FindFirstChild("Connected") -- value to check whether the plug is connected
if collectionService:HasTag(part, "Plug") and not debounce then
if connected and connected:IsA("BoolValue") and connected.Value then
return
end
debounce = true
plug = part -- set the nil plug value to the actual plug object
print(tostring(plug) .. " has plugged into " .. tostring(socket))
local plugPower = plug:FindFirstChild("Powered")
local dragDetector = plug:FindFirstChild("DragDetector")
plug.CFrame = hitBox.CFrame -- move the plug to the destination
createWeld(plug, hitBox)
updatePower(plugPower, socketPower)
dragDetector.Enabled = false
proximityPrompt.Enabled = true
connected.Value = true
end
end)
socketPower.Changed:Connect(function ()
if plug then
local plugPower = plug:FindFirstChild("Powered")
updatePower(plugPower, socketPower)
print("socket power" , socketPower.Value)
print("plug power" , plugPower.Value)
end
end)
proximityPrompt.Triggered:Connect(function ()
local plugPower = plug:FindFirstChild("Powered")
local dragDetector = plug:FindFirstChild("DragDetector")
local connected = plug:FindFirstChild("Connected")
connected.Value = false
if plugPower and plugPower:IsA("BoolValue") then
plugPower.Value = false
end
dragDetector.Enabled = true
proximityPrompt.Enabled = false
destroyWelds(plug)
plug = nil
wait(1)
debounce = false
end)
end