Script works fine on studio play mode, but breaks partially when in-game?
I am making a tower defense style game, and I made a minigunner tower for testing. But I am having a big problem.
On studio play mode, the tower's script works perfectly fine. But when in-game or on studio test mode, it breaks partially and the shoot() function doesn't fire for some reason.
Script:
02 | local players = game:GetService( "Players" ) |
03 | local debris = game:GetService( "Debris" ) |
06 | local tower = script.Parent.Parent |
08 | local towerHumanoidRootPart = tower:WaitForChild( "HumanoidRootPart" ) |
09 | local humanoid = tower:WaitForChild( "Humanoid" ) |
10 | local minigun = tower:WaitForChild( "Minigun" ) |
12 | local storage = tower:WaitForChild( "Storage" ) |
14 | local animations = storage:WaitForChild( "Animations" ) |
15 | local idleAnimation = animations:WaitForChild( "Idle" ) |
17 | local soundEffects = storage:WaitForChild( "SoundEffects" ) |
18 | local gunshotSFX = soundEffects:WaitForChild( "GunshotSFX" ) |
20 | local statistics = require(storage:WaitForChild( "TowerStatistics" )) |
21 | local range = statistics [ "Range" ] |
22 | local fireRate = statistics [ "FireRate" ] |
23 | local damage = statistics [ "Damage" ] |
26 | local function generateRangeMarker() |
27 | local rangeMarker = Instance.new( "Part" ) |
28 | rangeMarker.Anchored = true |
29 | rangeMarker.CanCollide = false |
30 | rangeMarker.CFrame = towerHumanoidRootPart.CFrame *CFrame.new( 0 , - 1.75 , 0 ) *CFrame.Angles( 0 , 0 , math.rad( 90 )) |
31 | rangeMarker.Size = Vector 3. new(. 1 , range * 2 , range * 2 ) |
32 | rangeMarker.BrickColor = BrickColor.new( "Deep blue" ) |
33 | rangeMarker.Transparency = . 5 |
35 | local rangeMarkerMesh = Instance.new( "SpecialMesh" , rangeMarker) |
36 | rangeMarkerMesh.MeshType = Enum.MeshType.Cylinder |
38 | rangeMarker.Parent = tower |
41 | local function startAnimation() |
43 | humanoid:LoadAnimation(idleAnimation):Play() |
47 | while wait(fireRate) do |
48 | local playerList = players:GetPlayers() |
50 | for i = 1 , #playerList do |
51 | local target = workspace:FindFirstChild(playerList [ i ] .Name) |
52 | if not target then return end |
54 | local targetHumanoid = target:FindFirstChild( "Humanoid" ) |
55 | if not targetHumanoid then return end |
57 | local targetHumanoidRootPart = target:FindFirstChild( "HumanoidRootPart" ) |
58 | if not targetHumanoidRootPart then return end |
60 | local magnitude = (towerHumanoidRootPart.CFrame.p - targetHumanoidRootPart.CFrame.p).magnitude |
62 | if magnitude < range then |
63 | towerHumanoidRootPart.CFrame = CFrame.new(towerHumanoidRootPart.CFrame.p, targetHumanoidRootPart.CFrame.p) |
64 | towerHumanoidRootPart.Orientation = Vector 3. new( 0 , towerHumanoidRootPart.Orientation.Y, towerHumanoidRootPart.Orientation.Z) |
66 | targetHumanoid:TakeDamage(damage) |
68 | local newGunshotSFX = Instance.new( "Sound" ) |
69 | newGunshotSFX.Name = "ClonedGunshotSFX" |
70 | newGunshotSFX.SoundId = gunshotSFX.SoundId |
72 | newGunshotSFX.Parent = minigun |
75 | debris:AddItem(newGunshotSFX, 1 ) |
This is the 3rd/4th time I am asking this, I didn't get a answer on the other times I asked this same question.