I am currently trying to make a cannon that the player can aim and shoot. It all works perfectly until the player gets out of the seat, then once the player gets back in the seat they can’t get out of it and they can’t use the cannon at all.
This uses three scripts, but there are two scripts that focus on making the player able to use it. After putting prints nearly every other line I noticed the second time a player uses it after stopping using it, it stops here:
Here are the two scripts:
Script:
local RS = game:GetService("ReplicatedStorage")
local SetCannonOwner = RS:WaitForChild("Events"):WaitForChild("SetCannonOwner")
local Model = script.Parent
local Cannon = Model:WaitForChild("Cannon")
local TouchPad = Model:WaitForChild("TouchPad")
local Controller = script:WaitForChild("CannonController")
inUse = false
playerUsing = nil
print("SS in Cannon Loaded Vars")
TouchPad.Touched:Connect(function(hit)
print("touch pad hit")
if game.Players:GetPlayerFromCharacter(hit.Parent) and inUse == false then
print("found player from hit part")
inUse = true
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Character = hit.Parent
local Root = Character:WaitForChild("HumanoidRootPart")
playerUsing = Player
print("Defined player")
Character.Humanoid.Sit = true
Root.Anchored = true
Root.CFrame = TouchPad.CFrame + Vector3.new(0, 2, 0)
print("sat player")
local c = Controller:Clone()
c.Parent = Character
c.Enabled = true
print("gave player localscript")
SetCannonOwner:FireClient(Player, Cannon, Controller)
print("fired setcannonowner")
end
end)
SetCannonOwner.OnServerEvent:Connect(function(Player, part)
print("SetCannonOwner fired from client")
if part == Cannon and inUse and playerUsing == Player then
print("SetCannonOwner fired from client is this cannon")
Player.Character:WaitForChild("HumanoidRootPart").Anchored = false
Player.Character:WaitForChild("CannonController"):Destroy()
print("take away controller and unanchor")
task.wait(3)
inUse = false
print("inuse again")
end
end)
CannonController(localscript)
local RNS = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")
local SetCannonOwner = RS:WaitForChild("Events"):WaitForChild("SetCannonOwner")
local SetCannonCFrame = RS:WaitForChild("Events"):WaitForChild("SetCannonCFrame")
local FireCannon = RS:WaitForChild("Events"):WaitForChild("FireCannon")
local player = PS.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local root = character:WaitForChild("HumanoidRootPart")
local Mouse = player:GetMouse()
local part
canShoot = true
print("loaded vars local")
local function UpdateCannonCFrame()
local pos = Vector3.new(Mouse.Hit.Position.X, part.Position.Y, Mouse.Hit.Position.Z)
SetCannonCFrame:FireServer(part, pos)
end
SetCannonOwner.OnClientEvent:Connect(function(Cannon, Controller)
part = Cannon
RNS:BindToRenderStep("UpdateCannonPos", 1, UpdateCannonCFrame)
print("player now moves the cannon")
end)
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and canShoot and part ~= nil then
canShoot = false
FireCannon:FireServer(part)
print("shot cannon")
task.wait(2)
print("can shoot again")
canShoot = true
end
end)
humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
RNS:UnbindFromRenderStep("UpdateCannonPos", 1, UpdateCannonCFrame)
SetCannonOwner:FireServer(part)
part = nil
print("unbinded can")
end)
humanoid.Died:Connect(function()
RNS:UnbindFromRenderStep("UpdateCannonPos", 1, UpdateCannonCFrame)
SetCannonOwner:FireServer(part)
part = nil
print("unbinded can")
end)