Script is "caching" variables

I made this cannon that when clicked clones a projectile part then launches it with ApplyImpulse.
Issue is… when I move the barrel(rotate, translation) it still fires in the same spot.

local model = script.Parent.Parent
local barrel = model.Barrel
local body = script.Parent
local clickDetector = model.ClickDetector
local projectile = model.Projectile

local function FireProjectile()
	local CPJ = projectile:Clone()
	CPJ.CFrame = barrel.CFrame:ToWorldSpace(,0,5))
	CPJ.Parent = game.Workspace

	CPJ:ApplyImpulse(barrel.CFrame.LookVector * 100)
	print(barrel.CFrame) -- Even though barrel is moved, it still prints the old value


Am I not properly understanding how variables work? I believe I’m holding a reference type, not a value type. It appears that variables

Try updating the barrel rotation on the server while in test mode.


1 Like

When you move things on the client, it doesn’t replicate onto the server. Switch the studio mode to server.