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

[Solved] Models colliding when I don't want them to?

Asked by
zblox164 531 Moderation Voter
5 years ago
Edited 5 years ago

I have a placement system that places parts and models along a grid. When I place models it works fine until the model is touching another object (Including the player). What happens is the model jumps on top of the object until you move your mouse off of the object. On most placement systems the models just move though the other objects but don't let you place the object. I am not asking for collision detection. I set CanCollideto false on every object in the model and still get the same result. Whats weird is that when I am placing a Part and set CanCollideto false it just moves through everything. Is there a simple way to make models move the same way that parts do?

Here is my code:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local PosX
local PosY = 2
local PosZ
local Model
local Toggle = nil
local Gui = script.Parent
local Button = Gui.TextButton
local Button2 = Gui.Model1
local IsPlacing = nil
local GridId = "rbxassetid://13786085"

local function SnapToGrid()
    local Snap = 2
    local Grid = 2
    local RoundVariable = 0.5

    PosX = math.floor(Mouse.Hit.X / Snap + RoundVariable) * Grid
    PosZ = math.floor(Mouse.Hit.Z / Snap + RoundVariable) * Grid
end

local function Placement()
    if Toggle == true and Model:IsA("Part") and Mouse.Target and Mouse.Target.Name == "Baseplate" then
        local PlacedModel = Model:Clone()
        PlacedModel.Parent = workspace.Clones
        Toggle = nil
        IsPlacing = nil
        game.Workspace.Baseplate.Texture.Texture  = ""
        Model.CanCollide = true
    elseif Toggle == true and Model:IsA("Model") then
        local PlacedModel = Model:Clone()
        PlacedModel.Parent = workspace.Clones
        Toggle = nil
        IsPlacing = nil
        game.Workspace.Baseplate.Texture.Texture  = ""
        local ModelProperties = PlacedModel:GetChildren()

        for _, Children in pairs(ModelProperties) do
            Children.CanCollide = false -- I even set CanCollide to false via the script
        end
    end
end

local function Movement()
    if IsPlacing == true and Model:IsA("Model") and Mouse.Target and Mouse.Target.Name == "Baseplate" then
        Mouse.TargetFilter = Model
        SnapToGrid()
        Model:MoveTo(Vector3.new(PosX, PosY, PosZ))
        Toggle = true
        local ModelTable = Model:GetChildren()

        for _, Children in pairs(ModelTable) do
            Children.CanCollide = false
            Children.Anchored = true
        end
    elseif IsPlacing == true and Model:IsA("Part") and Mouse.Target and Mouse.Target.Name == "Baseplate" then
        Mouse.TargetFilter = Model
        SnapToGrid()
        Model.Position = Vector3.new(PosX, PosY, PosZ)
        Toggle = true
        Model.CanCollide = false
        Model.Anchored = true
    end
end

local function StartPlacement1()
    if Player.leaderstats.Cash.Value >= 50 then
        game.Workspace.Baseplate.Texture.Texture = GridId
        Model = game.ReplicatedStorage.Movement:Clone()
        Model.Parent = workspace
        Mouse.Move:Connect(Movement)
        IsPlacing = true
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - 50
    end
end

local function StartPlacement2()
    if Player.leaderstats.Cash.Value >= 100 then
        game.Workspace.Baseplate.Texture.Texture = GridId
        Model = game.ReplicatedStorage.Tree:Clone()
        Model.Parent = workspace
        Mouse.Move:Connect(Movement)
        IsPlacing = true
        Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - 100
    end
end

Button.MouseButton1Click:Connect(StartPlacement1)
Button2.MouseButton1Click:Connect(StartPlacement2)
Mouse.Button1Down:Connect(Placement)

I don't know if there is a simple way to do this. Also i'm not asking for a whole placement script.

Thanks to who ever helps me!

0
Usually, setting the Position property of a part to go through another, puts it on top. You should instead CFrame it cringe_worthey 97 — 5y
0
Thanks! I gave it a try and it solved my problem! zblox164 531 — 5y

Answer this question