Help me rotate the object

hello everyone. I need to rotate the object that I’m creating with a script, but I still can’t do it, I can’t do it, I hope for your help

this thing is created from the wrong angle and it should be rotated away from the player, but I do not know how

I tried to change the Cframe through the script, but as a result it flew in the wrong direction

This local skript

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Gui = player.PlayerGui.slider
local Cursor = Gui.Frame.Cursor
local Char = player.Character
local increment = 0.01
local running = false 
local RopeEvent = script.Parent.RopeEvent
local holdingQ = false
local holdingE = false

local function TweenCursor(targetPosition, duration)
	local tweenCompleted = false
	Cursor:TweenPosition(targetPosition, "In", "Linear", duration, true, function()
		tweenCompleted = true
	end)
	while not tweenCompleted do
		wait() 
	end
end

script.Parent.Equipped:Connect(function()
	if Char:WaitForChild("Hook2") then
		Gui.Enabled = true
		running = true
		spawn(function() 
			while running do
				TweenCursor(UDim2.new(1, 0, 0.5), 2)
				if not running then break end
				TweenCursor(UDim2.new(0, 0, 0.5), 2)
			end
		end)
	end
end)

script.Parent.Activated:Connect(function()
	local Pos = Cursor.Position.X.Scale
	Pos = math.round(Pos / increment) * increment * 100
	script.Parent.RemEvent:FireServer(mouse.Hit.p, Pos)
end)

script.Parent.Unequipped:Connect(function()
	running = false 
	wait(0.1) 
	Gui.Enabled = false
	TweenCursor(UDim2.new(0, 0, 0.5), 0.01) 
end)

local UserInputService = game:GetService("UserInputService")


local function ContinuousFire(action)
	while holdingQ or holdingE do
		if holdingQ then
			RopeEvent:FireServer("Q") 
		elseif holdingE then
			RopeEvent:FireServer("E") 
		end
		wait(0.1) 
	end
end


UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.Q and not holdingQ then
				holdingQ = true
				spawn(function()
					ContinuousFire("Q") 
				end)
			elseif input.KeyCode == Enum.KeyCode.E and not holdingE then
				holdingE = true
				spawn(function()
					ContinuousFire("E")
				end)
			end
		end
	end
end)


UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.Q then
				holdingQ = false
				RopeEvent:FireServer("StopQ") 
			elseif input.KeyCode == Enum.KeyCode.E then
				holdingE = false 
				RopeEvent:FireServer("StopE")
			end
		end
	end
end)

And this is skript

local directon
local debris = game:GetService("Debris")
local MainPos 
local Force = 30  
local Handle = script.Parent.Handle
local hookthrow = false  
local player = script.Parent.Parent.Parent 
local Attachment1 = Instance.new("Attachment", Handle)
local RopeEvent = script.Parent.RopeEvent

script.Parent.RemEvent.OnServerEvent:Connect(function(player,mouse,Pos)
	if not hookthrow then
		directon = mouse
		MainPos = Pos

		local hook = Handle:Clone()
		hook.Parent = game.Workspace
		hook.Name = "hook_"..player.Name
		hook.CFrame = CFrame.new(script.Parent.Handle.Position + script.Parent.Handle.CFrame.LookVector * 1,directon)
		hook.CanCollide = true

		local Z = Force + MainPos
		hook.Velocity = hook.CFrame.LookVector * Z 

		local Attachment0 = Instance.new("Attachment",hook)
		Attachment0.Position =Vector3.new(1.1, 0, 0)


		local rope = Instance.new("RopeConstraint", Handle)
		rope.Length = 54
		rope.Visible = true
		rope.Thickness = 0.2
		rope.Attachment0 = Attachment0
		rope.Attachment1 = Attachment1
		Handle.Transparency = 1

		hookthrow = true 

	else

	end	
end)

script.Parent.Unequipped:Connect(function()
	local hook = game.Workspace:FindFirstChild("hook_".. player.Name)
	local rope = script.Parent.Handle:WaitForChild("RopeConstraint")
	Handle.Transparency = 0
	if hook then
		hook:Destroy()
		rope:Destroy()
	end
	hookthrow = false  
end)


local MaxRopeLength = 55
local MinRopeLength = 1

RopeEvent.OnServerEvent:Connect(function(player, keyPressed)
	local rope = script.Parent.Handle:WaitForChild("RopeConstraint")
	local hook = game.Workspace:FindFirstChild("hook_".. player.Name)

	if keyPressed == "Q" then
		if rope.Length > MinRopeLength then
			rope.Length = math.max(rope.Length - 1, MinRopeLength) 
			print("Rope length decreased to: " .. rope.Length) 
		end
	elseif keyPressed == "E" then
		if rope.Length < MaxRopeLength then
			rope.Length = math.min(rope.Length + 1, MaxRopeLength)
			print("Rope length increased to: " .. rope.Length) 
		else
			
			rope:Destroy()
			hook:Destroy()
			Handle.Transparency = 0
			hookthrow = false
			print("Rope destroyed because it exceeded maximum length.")
		end
	end
end)

yes, I may not be very good at programming, and therefore code edits are accepted if someone considers it bad

video how it works now

I tried to change the line
hook.CFrame = CFrame.new(script.Parent.Handle.Position + script.Parent.Handle.CFrame.LookVector * 1,directon) * CFrame.Angles(0, math.rad(90), 0)
but because of this, the hook began to fly away from where the player is looking
I also tried to change the cframe of handle

Tool grip editor the plugin is free and will help

the problem is not with the tool, I clone the handle and set it flying and the cloned object flies with the wrong orientation in space

you can see it in the video and I can’t imagine what needs to be changed in the code to make it work

make two tools, one with the hook, one without, and when player fires the hook, replace the old tool with the other tool

also you could just the handle as the tool with a hook, and when fired clone the tool and fire it, and make the hook parts in first part invisible

yes, I do, but you see, the object I’m cloning is not rotated correctly

hook.CFrame = CFrame.new(script.Parent.Handle.Position + script.Parent.Handle.CFrame.LookVector * 1,directon) * CFrame.Angles(0, math.pi / 2, 0)
hook.Velocity = (hook.CFrame * CFrame.Angles(0,-math.pi / 2,0)).LookVector * Z

It’s important to note that when you rotate the CFrame, it’s lookVector also changes, so what I did was subtract the rotation to get the original lookVector for the velocity
math.pi / 2 is basically math.rad(90)

You’re cool. I’m giving the solution to you. I couldn’t figure it out myself.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.