Hello everyone! Long story short, I have a script that pivots and sets an orientation to a part (laser) to that of an attachment.
Script that sends a signal to a second script
local localPlayer = game:GetService("Players").LocalPlayer
local uis = game:GetService("UserInputService")
local shootRemote = game:GetService("ReplicatedStorage"):WaitForChild("LaserShootEvent")
local cooldown = false
local bulletCooldown = false
local laserCooldown = false
local lasersShot = 0
repeat
task.wait()
until localPlayer.Character ~= nil
uis.InputBegan:Connect(function(input0, gamePros)
if input0.KeyCode == Enum.KeyCode.E and laserCooldown == false then
laserCooldown = true
repeat
shootRemote:FireServer(localPlayer.Name, false)
lasersShot += 1
task.wait(0.1)
until lasersShot >= 1000 -- must be 50
lasersShot = 0
laserCooldown = false
end
end)
Script that makes the laser to appear
local shootRemote = game:GetService("ReplicatedStorage"):WaitForChild("LaserShootEvent")
shootRemote.OnServerEvent:Connect(function(player0, player1, canShoot)
local character = workspace:FindFirstChild(player0.Name)
local rootCFrameX = character.HumanoidRootPart.ShootAttachment.WorldCFrame.X
local rootCFrameZ = character.HumanoidRootPart.ShootAttachment.WorldCFrame.Z
local newLaser = game:GetService("ServerStorage"):WaitForChild("Bullets").Laser1:Clone()
newLaser.Anchored = false
newLaser.Parent = workspace.Bullets
print(character.HumanoidRootPart.ShootAttachment.Orientation)
newLaser.Orientation = character.HumanoidRootPart.ShootAttachment.Orientation
newLaser:PivotTo(character.HumanoidRootPart.ShootAttachment.WorldCFrame)
newLaser:SetAttribute("PlayerToFollow", player0.Name)
if canShoot ~= nil then
character:SetAttribute("CanShoot", canShoot)
end
task.wait(0.1)
newLaser:Destroy()
end)
For some strange reason it behaves strangely when Y=90, Y=180:
I’m searching for an explanation of why it happens and a potential solution to the problem. Thanks for helping.
Is the shoot attachment’s orientation/position changing? To me it looks like there’s a trigonometric function (sin, cos, etc) somewhere causing the issue. When you put an angle of math.rad(180) into math.sin() it will always return zero. Similarly, an angle of, you guessed it, math.rad(90) into math.cos() will also always return zero. What’s likely happening is that since 0 cannot be negative or positive, the game cannot assign any directionality to the laser, and it defaults to the left and up directions. Consider using math.atan2(), which will return a number between -180 and 180.
Both orientation and position change, actually. But the problem wasn’t in one of the scripts I send. It was actually in an another one. Here how it looks like if anyone is curious:
while task.wait() do
local laser = script.Parent
local offset1 = Vector3.new(0, 0, laser.Size.X/2)
local offset2 = Vector3.new(laser.Size.X/2, 0, 0)
local playerToFollow = script.Parent:GetAttribute("PlayerToFollow")
local shootAttachment = workspace:FindFirstChild(playerToFollow).HumanoidRootPart.ShootAttachment.WorldCFrame
if laser.Orientation == Vector3.new(0, 90, 0) and script.Parent:GetAttribute("PlusOrMinus") == true then -- up
laser.CFrame = shootAttachment + offset1
elseif laser.Orientation == Vector3.new(0, 90, 0) and script.Parent:GetAttribute("PlusOrMinus") == false then -- down
laser.CFrame = shootAttachment - offset1
elseif laser.Orientation == Vector3.new(0, 180, 0) and script.Parent:GetAttribute("PlusOrMinus") == true then -- left
laser.CFrame = shootAttachment + offset2
elseif laser.Orientation == Vector3.new(0, 180, 0) and script.Parent:GetAttribute("PlusOrMinus") == false then -- right
laser.CFrame = shootAttachment - offset2
end
end
It was a child of a laser part.
“PlusOrMinus” variable is an abundance since it’s not used in the scripts above. I think it’s the main reason everything went south. It was always equal to True, and did not change, thus it messed things up. Still thank you, Robert, for help!