Hi! i am trying to do an aim assist script for the weapons kit of roblox (the third person ones) and it looks like it is not working fine…
As you can see on the video, the camera faces the dummy, but the actual gun keeps shooting at the original camera cframe and when you stop shooting the camera cframe returns back to the original cframe. Why? i try read the gun code and i could not get it, and the camera is always on scriptable, how i can fix this? why the guns dont shot where the camera looks?
this is the local script (inside the tool)
local RunService = game:GetService("RunService")
local tool = script.Parent
local camera = workspace.CurrentCamera
local studsRange = 100
local cameraRange = math.rad(30)
local function printDicctionary(table) for i, v in pairs(table) do warn(i) warn("") warn(v) end end
local function getCharactersArray(plrPos)
local characters = {}
for _, v in workspace:GetDescendants() do
if v:IsA("Humanoid") and v.Health > 0 and v.Parent:FindFirstChild("HumanoidRootPart") and (v.Parent.HumanoidRootPart.Position - plrPos).Magnitude <= studsRange and tool.Parent ~= v.Parent then
table.insert(characters,v.Parent.HumanoidRootPart.Position)
end
end
return characters
end
local function getDotProductOfPositions(positions, cameraLookVector, cameraPosition)
local dotArray = {}
for _, pos in pairs(positions) do
local posToPlayer = (pos - cameraPosition).Unit
local dot = posToPlayer:Dot(cameraLookVector)
dotArray[dot] = pos
end
return dotArray
end
local function getBiggerFromTable(table)
local greatestNumber = -2
for number, _ in pairs(table) do
if number > greatestNumber then
greatestNumber = number
end
end
return greatestNumber, table[greatestNumber]
end
tool.Activated:Connect(function()
local camera = workspace.CurrentCamera
local function beforeCamera(delta)
--warn(camera.CameraType)
local characters = getCharactersArray(camera:GetRenderCFrame().Position)
local dotArray = getDotProductOfPositions(characters,camera:GetRenderCFrame().LookVector,camera:GetRenderCFrame().Position)
local biggestDot,pos = getBiggerFromTable(dotArray)
--warn(biggestDot,pos)
if biggestDot > 0 then
camera.CFrame = CFrame.lookAt(camera:GetRenderCFrame().Position,pos)
end
end
RunService:BindToRenderStep("Before camera", Enum.RenderPriority.Camera.Value - 1, beforeCamera)
end)
tool.Deactivated:Connect(function()
RunService:UnbindFromRenderStep("tempBinding")
end)
This function should hold the answer to the question above, I found it by looking for a function for firing the weapon which was well named the same :fire(origin, dir, charge) with the parameters matching up from raycasting needs an origin and direction, therefore it should be correct.
function BulletWeapon:doLocalFire()
if self.tipAttach then
local tipCFrame = self.tipAttach.WorldCFrame
local tipPos = tipCFrame.Position
local aimDir = (self.aimPoint - tipPos).Unit
print("Local fire") -- will print indicating this is the direction the gun is firing in aimDir
self:fire(tipPos, aimDir, self.charge)
end
end
How the aim point is found through the lookvector on the attachment and not the camera direction. This is probably the cause:
local aimDir = tipCFrame.LookVector
local gunLookRay = Ray.new(tipCFrame.p, aimDir * 500)
local _, gunHitPoint = Roblox.penetrateCast(gunLookRay, self.ignoreList)
if self.weaponsSystem.aimRayCallback then
local _, hitPoint = Roblox.penetrateCast(self.weaponsSystem.aimRayCallback(), self.ignoreList)
self.aimPoint = hitPoint
else
self.aimPoint = gunHitPoint
end
Thanks for the reply! i still have problems even moving the fisical attachment every frame to orientate the same way as the camera or changing the tip direction to the camera cframe in the module script, its keeps facing the “fake camera”
I will keep trying, if i get it i will reply the script!
My code only aim the camera to the close dummy according to the camera lookvector, i tough that will be enogh to help the player shoot it, but as you see in the video it did not work, like dthecoolest say, is because the roblox rifle from the roblox weapons kit is coded to shot an attachment in the tool, no the camera, i already try changing the worldcframe of the attachment to be the camera cframe, but i cant make it change the orintation, when i have time i will look if the gun has a code piece that overwrites the attachment position
After a long research, find that i only need to change a line in the shoulderCamera script to make the gun shoot to the real camera that i was aiming tho the dummys, no other script is need to modify
To use the script put this local script in the tool and follow the instructions
-- in weapons system folder -> libraries folder -> ShulderCamera -> line 262:
--local cameraCFrame = self.currentCFrame
-- remplaze with
--local cameraCFrame = workspace.CurrentCamera:GetRenderCFrame()
local RunService = game:GetService("RunService")
local tool = script.Parent
local camera = workspace.CurrentCamera
local studsRange = 100
local cameraRange = math.rad(30)
local function printDicctionary(table) for i, v in pairs(table) do warn(i) warn("") warn(v) end end
local function getCharactersArray(plrPos)
local characters = {}
for _, v in workspace:GetDescendants() do
if v:IsA("Humanoid") and v.Health > 0 and v.Parent:FindFirstChild("HumanoidRootPart") and (v.Parent.HumanoidRootPart.Position - plrPos).Magnitude <= studsRange and tool.Parent ~= v.Parent then
table.insert(characters,v.Parent.HumanoidRootPart.Position)
end
end
return characters
end
local function getDotProductOfPositions(positions, cameraLookVector, cameraPosition)
local dotArray = {}
for _, pos in pairs(positions) do
local posToPlayer = (pos - cameraPosition).Unit
local dot = posToPlayer:Dot(cameraLookVector)
dotArray[dot] = pos
end
return dotArray
end
local function getBiggerFromTable(table)
local greatestNumber = -2
for number, _ in pairs(table) do
if number > greatestNumber then
greatestNumber = number
end
end
return greatestNumber, table[greatestNumber]
end
tool.Activated:Connect(function()
local camera = workspace.CurrentCamera
local function beforeCamera(delta)
--warn(camera.CameraType)
local characters = getCharactersArray(camera:GetRenderCFrame().Position)
local dotArray = getDotProductOfPositions(characters,camera:GetRenderCFrame().LookVector,camera:GetRenderCFrame().Position)
local biggestDot,pos = getBiggerFromTable(dotArray)
--warn(biggestDot,pos)
if biggestDot > 0.994 then
camera.CFrame = CFrame.lookAt(camera:GetRenderCFrame().Position,pos)
end
end
RunService:BindToRenderStep("aimbot", Enum.RenderPriority.Camera.Value - 1, beforeCamera)
end)
tool.Deactivated:Connect(function()
RunService:UnbindFromRenderStep("aimbot")
end)