I am having trouble fixing a arm pointing script for a gun on Roblox which causes the arm to do some weird motions and keep an orientation after dropping the tool or dying with the tool.
1 Like
Both scripts are located inside the gun
server script
local tool = script.Parent
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ArmAimEvent = ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("ArmAimEvent")
local aimingPlayers = {}
local function getRightShoulder(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.RigType == Enum.HumanoidRigType.R6 then
local torso = character:FindFirstChild("Torso")
if torso then
return torso:FindFirstChild("Right Shoulder")
end
end
return nil
end
local function calculateC0(torso, originalC0, mousePos)
local shoulderWorldPos = torso.CFrame:PointToWorldSpace(originalC0.Position)
local direction = (mousePos - shoulderWorldPos).Unit
local lookCFrame = CFrame.new(shoulderWorldPos, shoulderWorldPos + direction)
local localCFrame = torso.CFrame:ToObjectSpace(lookCFrame)
local rotationFix = CFrame.Angles(math.rad(90), math.rad(90), math.rad(-90))
return localCFrame * rotationFix
end
ArmAimEvent.OnServerEvent:Connect(function(player, mousePos)
local character = player.Character
if not character then return end
local shoulder = getRightShoulder(character)
if not shoulder then return end
if mousePos then
if not aimingPlayers[player] then
aimingPlayers[player] = {
originalC0 = shoulder.C0,
mousePos = mousePos,
}
else
aimingPlayers[player].mousePos = mousePos
end
else
if aimingPlayers[player] then
shoulder.C0 = aimingPlayers[player].originalC0
aimingPlayers[player] = nil
end
end
end)
RunService.Stepped:Connect(function()
for player, data in pairs(aimingPlayers) do
local character = player.Character
if character then
local torso = character:FindFirstChild("Torso")
local shoulder = getRightShoulder(character)
if torso and shoulder and data.mousePos then
local newC0 = calculateC0(torso, data.originalC0, data.mousePos)
shoulder.C0 = newC0
end
end
end
end)
local script
local tool = script.Parent
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local ArmAimEvent = ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("ArmAimEvent")
local connection
tool.Equipped:Connect(function()
connection = RunService.RenderStepped:Connect(function()
if mouse and mouse.Hit then
ArmAimEvent:FireServer(mouse.Hit.Position)
end
end)
end)
tool.Unequipped:Connect(function()
if connection then
connection:Disconnect()
connection = nil
end
ArmAimEvent:FireServer(nil) -- tell server to restore arm
end)
As you can see, I am a rookie coder so I attempted to use the built in ai to fix this code, i couldn’t reverse it so if anyone is willing to fix this script,
please help