Trying to set primarypart cframe does nothing?

Hello,

I am working on a turret where you sit and it turns to where your mouse is pointed, but I have a problem.
It doesn’t do anything.

I have it so a localscript gets cloned to the playergui when a player sits so it then via a RemoteEvent fires when the mouse moves and it has Mouse.Hit.Position as it’s parameter. Which is then hooked up on a server script that sets the primary part’s cframe.

Local Script:

local Players = game:GetService("Players")

local Player = Players.LocalPlayer

local Obj = script.Obj

local Mouse = Player:GetMouse()

Mouse.Move:Connect(function()

Obj.Value.Seat.Handler.RemoteEvent:FireServer(Mouse.Hit.Position)

end)

Server Script:

local Players = game:GetService("Players")
local Seat = script.Parent
local Active = false
local NewScript = nil

script.RemoteEvent.OnServerEvent:Connect(function(Player, MouseP)
	script.Parent.Parent.Guns.Gun:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.Guns.Gun.PrimaryPart.CFrame.Position, MouseP))
	script.Parent.Parent.Guns.Gun2:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.Guns.Gun2.PrimaryPart.CFrame.Position, MouseP))
end)

Seat.ChildAdded:Connect(function(x)
	if not Active then
		if x.Name == "SeatWeld" then
			Active = true
			local Player = Players:GetPlayerFromCharacter(x.Part1.Parent)
			if Player then
				if Player.PlayerGui then
					NewScript = script.LocalScript:Clone()
					NewScript.Parent = Player.PlayerGui
					NewScript.Obj.Value = Seat.Parent
					NewScript.Disabled = false
				end
			end
		end
	end
end)

Seat.ChildRemoved:Connect(function(x)
	if x.Name == "SeatWeld" then
		if Active then
			Active = false
			pcall(function() NewScript:Destroy() end)
			NewScript = nil
		end
	end
end)

What’s the issue?

1 Like

First I’d put a print statement in this fn to make sure it’s being called when you think it is.

script.RemoteEvent.OnServerEvent:Connect(function(Player, MouseP)
	print("Event is Firing!")
 
script.Parent.Parent.Guns.Gun:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.Guns.Gun.PrimaryPart.CFrame.Position, MouseP))
script.Parent.Parent.Guns.Gun2:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.Guns.Gun2.PrimaryPart.CFrame.Position, MouseP))
end)

And also plug in some values manually for MouseP to make sure you can move the parts this way. Checking each function/block to make sure it works using prints and manual input will help narrow down the problem. That will save someone else from having to construct a test scene for this kind of thing:

script.Parent.Parent.Guns.Gun2:SetPrimaryPartCFrame(CFrame.new(script.Parent.Parent.Guns.Gun2.PrimaryPart.CFrame.Position

It does fire though. That’s what is weird.

Oh, that’s good. So the problem is likely with those SetPrimaryPartCFrame calls. Can you move the guns with known value you pass in for MouseP?

or try printing to see if it’s what you expect
print(script.Parent.Parent.Guns.Gun.PrimaryPart.CFrame.Position, MouseP)

I tried that too, everything printed out normally.

Try adjusting testMouse value here (and removing the comment).
Select the gun in studio and run on command line to see if it moves.

local testMouse = Vector.new(0,0,0) -- some test value
local sel = game:GetService("Selection"):Get()[1];
sel:SetPrimaryPartCFrame(CFrame.new(sel.PrimaryPart.CFrame.Position, testMouse))

It moves now.

30 characteers