RenderStepped = RunService.RenderStepped:Connect(function()
local Ignore = {ItemClone, PlayersPlot.House, Players[PlayersPlot.Name].Character}
local Hit, Position = GetMouseHit(Ignore)
if Hit then
if Hit:IsDescendantOf(PlayersPlot) then
if Hit.Name == 'Base' then
for _, v in pairs(GetTouchingParts(ItemClone.Hitbox)) do
if not v:IsDescendantOf(ItemClone) then
if v.Name ~= 'Teleport' then
if not v:IsDescendantOf(Players:FindFirstChild(PlayersPlot.Name).Character) then
ItemClone.Hitbox.Transparency = 0.5
break
end
end
end
local cFrame = Place(Hit, ItemClone, Position, _G.Rotation)
ItemClone:SetPrimaryPartCFrame(cFrame)
ItemClone.Hitbox.Transparency = 1
end
end
end
end
end)
Basically, I got it working so if the model is touching other parts, its hitbox turns red (to signify you can’t place it) However what I want to happen is if it’s colliding with something then dont let it move.
So basically, if its not colliding, it can move anywhere, but if you try moving it into another object, the model just stops and wont go any further
What you need to do is not set the position when the object collides, so do this:
Once object collision is detected, it breaks the loop to prevent the position from being set.
RenderStepped = RunService.RenderStepped:Connect(function()
local Ignore = {ItemClone, PlayersPlot.House, Players[PlayersPlot.Name].Character}
local canSet = true
local cFrame = false
local Hit, Position = GetMouseHit(Ignore)
if Hit then
if Hit:IsDescendantOf(PlayersPlot) then
if Hit.Name == 'Base' then
for _, v in pairs(GetTouchingParts(ItemClone.Hitbox)) do
if not v:IsDescendantOf(ItemClone) then
if v.Name ~= 'Teleport' then
if not v:IsDescendantOf(Players:FindFirstChild(PlayersPlot.Name).Character) then
canSet = false
break
end
end
end
cFrame = Place(Hit, ItemClone, Position, _G.Rotation)
end
end
end
end
if canSet and cFrame then
ItemClone:SetPrimaryPartCFrame(cFrame)
ItemClone.Hitbox.Transparency = 1
end
end)
Sorry about indenting, I am on mobile
Basically, your problem was that if a collision was detected after the position was set originally, it would still move. Now, it only sets if there is no collision at all.
1 Like
Hmm ok, problem that occurs with this though is that when the model does collide you cant move it anymore, it becomes stuck.
I thought that is what you wanted?
but if you try moving it into another object, the model just stops and wont go any further
As in like the model wont go further into the part. So it can move anywhere at all times, but if its about to collide with something it should stay at its previous position until the player moves their mouse and the model starts moving along with it
Then it has to be something with your collision. All the variables are redefined every loop.
local Ignore = {ItemClone, PlayersPlot.House, Players[PlayersPlot.Name].Character}
local canSet = true
local cFrame = false
local Hit, Position = GetMouseHit(Ignore)
So it has to be something with your collision. My code should work as long as you collision does; I tested it with my collision detection.