Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Script works fine on studio play mode, but breaks partially when in-game?

Asked by 5 years ago

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:

01--//Services
02local players = game:GetService("Players")
03local debris = game:GetService("Debris")
04 
05--//Variables
06local tower = script.Parent.Parent
07 
08local towerHumanoidRootPart = tower:WaitForChild("HumanoidRootPart")
09local humanoid = tower:WaitForChild("Humanoid")
10local minigun = tower:WaitForChild("Minigun")
11 
12local storage = tower:WaitForChild("Storage")
13 
14local animations = storage:WaitForChild("Animations")
15local idleAnimation = animations:WaitForChild("Idle")
View all 84 lines...

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.

0
im not gonna get fully in to this but maybe one thing that u can change and probably should change too is when u got magnitude checks, dont check using CFrame, check using HumanoidRootPart.Position Gameplayer365247v2 1055 — 5y

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
5 years ago

This lines:

1if not target then return end
2if not targetHumanoid then return end

are ending your function completely rather than restarting the loop. You can either replace return with break that will break the for loop, or nest your code few more times, like this:

01for i = 1, #playerList do
02 
03    local target = workspace:FindFirstChild(playerList[i].Name)
04    if target then
05 
06        local targetHumanoid = target:FindFirstChild("Humanoid")
07        if targetHumanoid then
08 
09            local targetHumanoidRootPart = target:FindFirstChild("HumanoidRootPart")
10            if targetHumanoidRootPart then
11 
12                local magnitude = (towerHumanoidRootPart.CFrame.p - targetHumanoidRootPart.CFrame.p).magnitude
13 
14                if magnitude < range then
15 
View all 34 lines...
0
Living legend. Thanks alot man! I was having this issue for a very long time and I am glad to have finally fixed it. Cheers! arthurdaniel91 72 — 5y
Ad

Answer this question