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

Why does this scripts "If" statement now work properly?

Asked by 3 years ago

i have been making a building system for a game im developing. but i have run into an issue where the if statement doesnt work properly

if you want the full code here it is:

local mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local tp = script.TP

local build = require(game.ReplicatedStorage:WaitForChild("Build"))

local placing = false

local mousemoved = false

local place = false

local pos = nil

local placeable = true

mouse.Move:Connect(function()
    mousemoved = true
end)

local function GetTouchingParts(part)
    local connection = part.Touched:Connect(function() end)
    local results = part:GetTouchingParts()
    connection:Disconnect()
    return results
end

mouse.Button1Down:Connect(function()
    if placing == true and placeable == true then
        place = true
        wait(0.1)
        build.Build(script.Parent.Parent.BUILDING.Value,pos)
        wait(0.1)
        pos = nil
        placing = false
        place = false
    end
end)

script.Parent.MouseButton1Click:Connect(function()
    if placing == false then
        wait(0.1)

        placing = true
        local building = script.Parent.Parent.BUILDING.Value:Clone()
        building.Parent = workspace

        print(building.Body.Hitbox:GetTouchingParts())

        local buildingtp = tp:Clone()
        buildingtp.Parent = workspace

        while true do
            if mousemoved == true then
                if placeable == true then
                    buildingtp.Position = mouse.hit.Position
                    building.Body:SetPrimaryPartCFrame(CFrame.new(buildingtp.CFrame.X,building.TRAITS.OFFSET.Value.Y,buildingtp.CFrame.Z),buildingtp)
                    mousemoved = false
                end
            end
            if place == true then
                place = false
                pos = building.Body:GetPrimaryPartCFrame()
                building:Remove()
                buildingtp:Remove()
                break
            end
            print(GetTouchingParts(building.Body.Hitbox))
            for i,v in pairs(GetTouchingParts(building.Body.Hitbox)) do
                print(v.Name)
                if v.Name == "Hitbox" then --Problem Here
                    --it will never ever go here
                    building.Body.Hitbox.Transparency = 0.5
                    building.Body.Hitbox.BrickColor = BrickColor.new("Really red")
                    placeable = false
                    print("true")
                else
                    --no matter what it goes here:
                    building.Body.Hitbox.Transparency = 1
                    placeable = true
                    print("false") 
                end
            end
            wait()
        end
    end
end)

so i really hope someone will help with this

thanks :)

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I am not super experienced with scripting, but I think you got some bad lines of code

Bruh, I got very confused from these variables.Try to completely read ur code and make sure that variable changing true or false is good, imagine the script running. error will be there. In this version I rewrited your GetTouchingParts function, i think this is better way.

local mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local tp = script.TP

local build = require(game.ReplicatedStorage:WaitForChild("Build"))

local placing = false

local mousemoved = false

local place = false

local pos = nil

local placeable = true

local touchingPartsTable = {}

mouse.Move:Connect(function()
    mousemoved = true
end)

local function GetTouchingParts(part)
    for i, v in pairs(touchingPartsTable) do
        table.remove(v)
    end

    local connection = part.Touched:Connect(function(obj)
        table.insert(touchingPartsTable, obj) 
    end)
    wait()
    connection:Disconnect()
    return touchingPartsTable
end

mouse.Button1Down:Connect(function()
    if placing == true and placeable == true then
        place = true
        wait(0.1)
        build.Build(script.Parent.Parent.BUILDING.Value,pos)
        wait(0.1)
        pos = nil
        placing = false
        place = false
    end
end)

script.Parent.MouseButton1Click:Connect(function()
    if placing == false then
        wait(0.1)

        placing = true
        local building = script.Parent.Parent.BUILDING.Value:Clone()
        building.Parent = workspace

        print(building.Body.Hitbox:GetTouchingParts())

        local buildingtp = tp:Clone()
        buildingtp.Parent = workspace

        while true do
            if mousemoved == true then
                if placeable == true then
                    buildingtp.Position = mouse.hit.Position
                    building.Body:SetPrimaryPartCFrame(CFrame.new(buildingtp.CFrame.X,building.TRAITS.OFFSET.Value.Y,buildingtp.CFrame.Z),buildingtp)
                    mousemoved = false
                end
            end
            if place == true then
                place = false
                pos = building.Body:GetPrimaryPartCFrame()
                building:Remove()
                buildingtp:Remove()
                break
            end
            print(GetTouchingParts(building.Body.Hitbox))
            for i,v in pairs(GetTouchingParts(building.Body.Hitbox)) do
                print(v.Name)
                if v.Name == "Hitbox" then --Problem Here
                    --it will never ever go here
                    building.Body.Hitbox.Transparency = 0.5
                    building.Body.Hitbox.BrickColor = BrickColor.new("Really red")
                    placeable = false
                    print("true")
                else
                    --no matter what it goes here:
                    building.Body.Hitbox.Transparency = 1
                    placeable = true
                    print("false") 
                end
            end
            wait()
        end
    end
end)
Ad

Answer this question