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

Touched event not working when updating a region3?

Asked by
TGazza 1336 Moderation Voter
3 years ago

Hi guys. im kinda new here. Been looking for a roblox help fourm for a while now and found this site. weird verification thingy .... o.0!

Anyway My Problem: i've written a script that clones a part or a model depending on how a user configures it.

The script checks for 2 things,the checks are as follows:

1) check if the script can find the model to be cloned

2) (Optional) Check to see if the user wants to use a button to clone the model with ie: click a ClickDetector/ touch a Part or using one of the Gui Element buttons to clone the model.

If the user hasn't got a button, the script will use a 5 sec timer to check for the spawn point of the model to be cloned to see if its clear. If its not then just sleep.

The problem im having is when you touch a part in the workspace it keeps saying the spawn point is not clear when in reality i've removed the model from the workspace!

For the life of me im unsure what is going on but when you dont specify a button the script works as intended! But with a button it seems to think the model is still at the spawn point... o.0!

i know in the past this would work but am i calling the Touch event wrong or do i need to envoke the server from local script inside the button or player? im confused lol

The code i wrote (in full!) is below

Code:

--// name of your model or part you want to clone
local Model_Or_Part_Name = "Observation Tower"
--// change this to the button your using (optional!)
local Button_Name = "Part"

local CloneMETime = 5 --// in seconds!
local CloneDisableTime = 5 --// time to disable the button in seconds, this is to stop spamming of the button (if set!)
--// this is for your button if its touchable ingame!
local TouchedColor = Color3.fromRGB(0,0,0)

--[[
##############################################################################################################
#################### Don't change anything below this unless you know what your are doing!####################
##############################################################################################################
]]

local OriginalColor = TouchedColor
local model = (function()
    local Chk = script.Parent:FindFirstChild(Model_Or_Part_Name) 
    if(Chk == nil) then
        error("Error: Model_Or_Part_Name Varable is not set\n replace [\" Model_Or_Part_Name \"] With the name of the model or part you want to clone on Line 1 and try again!")
    end
    return Chk
 end)()

local btn = (function()
    local Chk = script.Parent:FindFirstChild(Button_Name) 
    if(Chk == nil) then
        warn("Error: Button_Name Varable is not set\n  \t replace \"Button\" With the name of the Button you want to use and try again!")
        return nil
    end
    if(Chk:IsA("BasePart") == true) then
        OriginalColor = Chk.Color
    end
    return Chk
 end)()

local Debounce = false
local Cpy = model:Clone()

local ModelPosition,ModelSize = (function()
    local isModel = model:IsA("Model")
    if(isModel == true) then
        local Bonding_BoxCFrame, Bonding_Box = model:GetBoundingBox()
        return Bonding_BoxCFrame.Position,Bonding_Box
    else
        return model.Position, model.Size
    end
end)()

local function isAreaEmpty()
    --// the following code is taken from the roblox wiki 
    --// https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3
    --// Just modified to work with models and Parts
    local SA = ModelPosition - (0.5 * (ModelSize*0.95))
    local SB = ModelPosition + (0.5 * (ModelSize*0.95))
    return workspace:IsRegion3Empty(Region3.new(SA, SB))
end


local function Cloneit(Model)
    local C = Cpy:Clone()
    C.Parent = workspace
    C:MakeJoints()
end

local function DoTheDeed()
    local isClear = isAreaEmpty()
    if(Debounce == false) then
        Debounce = true
        if(isClear == true) then
            if(btn ~= nil and btn:IsA("BasePart") == true) then
                btn.Color = TouchedColor
            end 
            print("Quick Spawn point is Clear \n Spawning clone...")
            Cloneit(Cpy)
        else
            if(btn ~= nil and btn:IsA("BasePart") == true) then
                btn.Color = Color3.fromRGB(255,0,0)
            end
            print("Spawn point is NOT clear waiting for spawn point to become clear...")
        end
        wait(CloneDisableTime)
        if(btn ~= nil and btn:IsA("BasePart") == true) then
            btn.Color = OriginalColor
        end
        Debounce = false
    end
end

local Hooked_Up = false
if(btn ~= nil) then
    if(btn:IsA("BasePart") == true) then
        btn.Touched:connect(function(part)
            local isHuman = (part ~= nil and part.Parent:FindFirstChildOfClass("Humanoid") ~= nil) and true or false
            if(isHuman == true) then --// something with a humanoid has touched this button!
                --// lets try to make a clone of my model!
                DoTheDeed()
            end 
        end)
        Hooked_Up = true
    end
    if(btn:IsA("ClickDetector") == true) then
        btn.MouseClick:connect(function(playerWhoClickedME)
            print(playerWhoClickedME.." Clicked me! \n spawning model!")
            DoTheDeed()
        end)
        Hooked_Up = true
    end
    if(btn:IsA("GuiButton") == true or btn:IsA("TextButton") == true or btn:IsA("ImageButton") == true)then
        btn.MouseButton1Click:Connect(DoTheDeed)
        btn.TouchTap:Connect(DoTheDeed)
        Hooked_Up = true
    end 
end
if(Hooked_Up == true) then
    print("Model Cloner is Ready for operation!")
else
    warn("Model Cloner couldn\'t find a suitible button\n Running Checker every ",CloneMETime," secs")
    while true do
        DoTheDeed()
        wait(CloneMETime)
    end
end

Any help would be appreciated!

Answer this question