I have a script that checks if specific requirements are met through RunService.RenderStepped: and when these requirements are met it theoretically should bind an action through ContextActionService. But for some reason it does not happen, even though the print that is located just near the bind works. Here is a full code:
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local highlight = ReplicatedStorage.PlayersHighlight
local dragPart = ReplicatedStorage.DragPart
local function GetParts(chosenPart)
local toCheck = {chosenPart}
local checked = {}
while #toCheck > 0 do
for i, part in pairs(toCheck) do
if table.find(checked, part) then
continue
else
local directlyConnectedParts = part:GetConnectedParts(true)
local indirectlyConnectedParts = {}
local joints = part:GetJoints()
for i, joint in pairs(joints) do
if joint:IsA("HingeConstraint") then
local hinge = joint
if hinge.Attachment0.Parent == part then
if not table.find(toCheck, hinge.Attachment1.Parent) then
table.insert(indirectlyConnectedParts, hinge.Attachment1.Parent)
end
elseif hinge.Attachment1.Parent == part then
if not table.find(toCheck, hinge.Attachment0.Parent) then
table.insert(indirectlyConnectedParts, hinge.Attachment0.Parent)
end
end
end
end
for i, con in pairs(indirectlyConnectedParts) do
table.insert(toCheck, con)
end
for i, v in pairs(directlyConnectedParts) do
if not table.find(toCheck, v) then
table.insert(toCheck, v)
end
end
table.insert(checked, part)
end
end
if #toCheck == #checked then
break
end
end
return checked
end
local function Drag(inputObject, inputState, part, mousePosition)
if inputObject == Enum.UserInputType.MouseButton1 and inputState == Enum.UserInputState.Begin then
local distance = (player.Character:FindFirstChild("HumanoidRootPart").Position - mousePosition).Magnitude
dragPart.Parent = workspace
dragPart.Position = mousePosition
local weld = Instance.new("WeldConstraint")
weld.Part0 = dragPart
weld.Part1 = part
weld.Parent = dragPart
RunService.RenderStepped:Connect(function()
dragPart.Position = Vector3.new(camera.CFrame.X + camera.CFrame.LookVector.X * distance, camera.CFrame.Y + camera.CFrame.LookVector.Y * distance, camera.CFrame.Z + camera.CFrame.LookVector.Z * distance)
end)
end
end
RunService.RenderStepped:Connect(function()
local mouseTarget = mouse.Target
highlight.Adornee = mouseTarget
if mouseTarget ~= nil then
if (player.Character:FindFirstChild("HumanoidRootPart").Position - mouseTarget.Position).Magnitude < 20 then
if mouseTarget.Anchored ~= true then
if mouseTarget:FindFirstChild("ClickDetector") == nil then
local allConnectedParts = GetParts(mouseTarget)
local connectedParts = mouseTarget:GetConnectedParts(true)
local anchoredConnections = {}
local occupiedSeats = {}
for i, part in pairs(allConnectedParts) do
if part:IsA("Seat") or part:IsA("VehicleSeat") then
if part.Occupant ~= nil then
table.insert(occupiedSeats, part)
end
end
end
for i, part in pairs(connectedParts) do
if part.Anchored == true then
table.insert(anchoredConnections, part)
end
end
if #anchoredConnections == 0 then
if #occupiedSeats == 0 then
ContextActionService:BindAction("Dragging", Drag, false, Enum.UserInputType.MouseButton1) -- This bind does not work
ReplicatedStorage.PlayersHighlight.Enabled = true
else
ReplicatedStorage.PlayersHighlight.Enabled = false
end
else
ReplicatedStorage.PlayersHighlight.Enabled = false
end
else
ReplicatedStorage.PlayersHighlight.Enabled = false
end
else
ReplicatedStorage.PlayersHighlight.Enabled = false
end
else
ReplicatedStorage.PlayersHighlight.Enabled = false
end
else
ReplicatedStorage.PlayersHighlight.Enabled = false
end
end)
Your action might be getting sunk, try binding it at higher priority or use UserInputService instead.
Also quick tip to clean up your code and make it more readable, combine your checks using and and use early returns to reduce nested if-then statements.
Doesn’t BindAction only bind the action to the specified key? You still have to do that action, no?
For example you’re binding to MouseButton1. It probably won’t work if you’re already holding the button.
I have recreated my old attempt of doing what you basically said. Here is how it looks:
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local highlight = ReplicatedStorage.PlayersHighlight
local function GetParts(chosenPart)
local toCheck = {chosenPart}
local checked = {}
while #toCheck > 0 do
for i, part in pairs(toCheck) do
if table.find(checked, part) then
continue
else
local directlyConnectedParts = part:GetConnectedParts(true)
local indirectlyConnectedParts = {}
local joints = part:GetJoints()
for i, joint in pairs(joints) do
if joint:IsA("HingeConstraint") then
local hinge = joint
if hinge.Attachment0.Parent == part then
if not table.find(toCheck, hinge.Attachment1.Parent) then
table.insert(indirectlyConnectedParts, hinge.Attachment1.Parent)
end
elseif hinge.Attachment1.Parent == part then
if not table.find(toCheck, hinge.Attachment0.Parent) then
table.insert(indirectlyConnectedParts, hinge.Attachment0.Parent)
end
end
end
end
for i, con in pairs(indirectlyConnectedParts) do
table.insert(toCheck, con)
end
for i, v in pairs(directlyConnectedParts) do
if not table.find(toCheck, v) then
table.insert(toCheck, v)
end
end
table.insert(checked, part)
end
end
if #toCheck == #checked then
break
end
end
return checked
end
local MapCheck = false
local RangeCheck = false
local ButtonCheck = false
local AnchorCheck = false
local SeatsCheck = false
RunService.RenderStepped:Connect(function()
local target = mouse.Target
highlight.Adornee = target
if target ~= nil then
if target.Parent ~= workspace.Map then
MapCheck = true
end
if (player.Character:FindFirstChild("HumanoidRootPart").Position - target.Position).Magnitude <= 20 then
RangeCheck = true
end
if target:FindFirstChild("ClickDetector") == nil then
ButtonCheck = true
end
local allConnectedParts = GetParts(target)
local connectedParts = target:GetConnectedParts(true)
local anchoredConnections = {}
local occupiedSeats = {}
for i, part in pairs(connectedParts) do
if part.Anchored == true then
table.insert(anchoredConnections, part)
end
end
if #anchoredConnections == 0 then
AnchorCheck = true
end
for i, part in pairs(allConnectedParts) do
if part:IsA("Seat") or part:IsA("VehicleSeat") then
if part.Occupant ~= nil then
table.insert(occupiedSeats, part)
end
end
end
if #occupiedSeats == 0 then
SeatsCheck = true
end
if MapCheck == true and RangeCheck == true and ButtonCheck == true and AnchorCheck == true and SeatsCheck == true then
highlight.Enabled = true
else
highlight.Enabled = false
end
else
highlight.Enabled = false
end
end)
The highlight is always active, no matter where you aim.
Edit, nvm it works quite fine once I added else statements that change the Checks to false. Forgot to add them at first
Yeah, but when I click it does not work! May be the function is actually being binded, because click detectors don’t work when the script is active, but even putting something as simple as print(“clicked”) inside that function does not give any result
Try to simply add another BindAction outside your main code at the bottom, like TestDrag or something.
In the drag function, add a print statement print("Triggered:", actionName, inputState, inputObject.UserInputType). If this works then its a issue with your timing.