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

Loop Not Running on Second Attempt. What's Wrong?

Asked by 2 years ago

I'm trying to make a tower defense game, and I'm trying create a character placing system. When I run it once, it works fine, but on the second time, it doesn't run at all and just stops around line 17.

What am I doing wrong?

local players = game:GetService("Players")
local plr = players.LocalPlayer
local mouse = plr:GetMouse()

local mouseDown = false

local uis = game:GetService("UserInputService")

script.Parent.MouseButton1Click:Connect(function()
    if script.Parent.Parent.Parent.Parent:WaitForChild("Money").Value > 199 then
        script.Parent.Parent.Parent.Enabled = false

        script.Parent.Parent.Parent.Parent:WaitForChild("Money").Value = script.Parent.Parent.Parent.Parent:WaitForChild("Money").Value - 200

        local character = game.ReplicatedStorage.Characters.Character:Clone()

        character.Parent = workspace

        repeat wait()
            local camera = workspace.CurrentCamera

            local params = RaycastParams.new()
            params.FilterDescendantsInstances = {plr.Character, character}
            params.FilterType = Enum.RaycastFilterType.Blacklist

            local function rayResult(x, y)
                local unitRay = mouse.UnitRay

                return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)
            end

            local mousePos = uis:GetMouseLocation()

            if rayResult(mousePos.X, mousePos.Y) then
                character:SetPrimaryPartCFrame(CFrame.new(rayResult(mousePos.X, mousePos.Y).Position) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(90)))
            end

            print("Running")

            if mouse.Target then
                print("Target")

                if mouse.Target.Name == "Placeable" then                    
                    character.Body.Body.Color = ColorSequence.new(Color3.fromRGB(0, 255, 0))
                    character.PrimaryPart.Color = Color3.fromRGB(0, 255, 0)
                else
                    character.Body.Body.Color = ColorSequence.new(Color3.fromRGB(255, 0, 0))
                    character.PrimaryPart.Color = Color3.fromRGB(255, 0, 0)
                end
            else
                character.Body.Body.Color = ColorSequence.new(Color3.fromRGB(255, 0, 0))
                character.PrimaryPart.Color = Color3.fromRGB(255, 0, 0)
            end
        until mouseDown == true

        local camera = workspace.CurrentCamera

        local params = RaycastParams.new()
        params.FilterDescendantsInstances = {plr.Character, character}
        params.FilterType = Enum.RaycastFilterType.Blacklist

        local function rayResult(x, y)
            local unitRay = mouse.UnitRay

            return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)
        end

        local mousePos = uis:GetMouseLocation()

        if character.PrimaryPart.Color == Color3.fromRGB(0, 255, 0) then
            if rayResult(mousePos.X, mousePos.Y) then
                game.ReplicatedStorage.PlaceCharacter:FireServer("Character", CFrame.new(rayResult(mousePos.X, mousePos.Y).Position) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(90)))
            end

            script.Parent.Parent.Parent.Enabled = true
        end

        character:Destroy()
    end
end)

mouse.Button1Down:Connect(function()
    mouseDown = true
end)

mouse.Button1Up:Connect(function()
    mouseDown = false
end)
0
Using a print, I was able to see that the code would just stop before the loop. EdiCat1234 0 — 2y
0
do you see errors in the output? if not try making print between line 9 and 10 and at line 12, which of them prints? imKirda 4491 — 2y

Answer this question