I am making a game where you can become a ghost.
I’m not gonna go too much into detail but the main aspect is it’s a puzzle game where you can go through certain walls as a ghost but you also have some limitations, basically you have to figure out when you need to be a ghost and when you need to be human.
I’m also making multiplayer mode, but I want to make it so that ghosts can go through humans and other ghosts, but humans can’t go through other humans, which I’m having trouble doing.
This is my current code, which is like half working but always has some sort of problem in it:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ghostPlayer")
local PhysicsService = game:GetService("PhysicsService")
PhysicsService:CreateCollisionGroup(Players)
remoteEvent.OnServerEvent:Connect(function(player)
game.Players.PlayerAdded:Connect(function(player)
PhysicsService:CreateCollisionGroup(player.Name)
end)
for i, charPart in pairs(player.Character:GetDescendants()) do
if charPart:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(charPart, player.Name)
end
end
local function playerCollisionHandler()
local pName = player.Name
for _, plr in pairs(game.Players:GetChildren()) do
if plr.Name ~= pName then
for i, charPart in pairs(plr.Character:GetDescendants()) do
if charPart:IsA("BasePart")then
PhysicsService:SetPartCollisionGroup(charPart, Players)
if charPart.Transparency == 0 then
PhysicsService:CollisionGroupSetCollidable(player.Name, Players, false)
else
PhysicsService:CollisionGroupSetCollidable(player.Name, Players, true)
end
end
end
end
end
end
playerCollisionHandler()
game.Players.PlayerAdded:Connect(function(player)
playerCollisionHandler()
end)
end)
There is a bit more, but it’s just wall collision stuff which is working fine.
If anyone needs to see the rest of the code I’m okay with putting it in there.
Also: Sorry my code is messy, I normally clean it up after I get it working.
Instead of having a collision group per player, have one for ghosts and humans and set the collisions respectively.
I didn’t think about this, let me try it out really quickly
Sorry for the deleted post above, it was a mistake I made but then realized the fix for.
For some reason this still isn’t preventing them from colliding.
I checked the console both in game with me and my friend and in studio and I didn’t get any errors, I also made it print what and when it changed the collision group for something and it seemed to be working fine for that.
I have monkey brain so it could just be an obvious mistake that I’m not seeing, but here’s the code in case you want to check over it.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ghostPlayer")
local PhysicsService = game:GetService("PhysicsService")
PhysicsService:CreateCollisionGroup("Humans")
PhysicsService:CreateCollisionGroup("Ghosts")
PhysicsService:CollisionGroupSetCollidable("Humans", "Ghosts", false)
PhysicsService:CollisionGroupSetCollidable("Ghosts", "Ghosts", false)
game.Players.PlayerAdded:Connect(function(player)
local character = player.CharacterAdded:Wait()
for i, v in pairs(character:GetDescendants()) do
print("Changed "..v.Name.."'s collision group to Humans")
PhysicsService:SetPartCollisionGroup(v, "Humans")
end
end)
function ghostPlayer(player)
local Character = player.Character
for i, v in pairs(Character:GetDescendants()) do
if v:IsA("BasePart") or v:IsA("Decal") then
if v.Name ~= "HumanoidRootPart" and v.Name ~= "Handle" then
if v.Transparency == 0 then
if v:IsA("BasePart") then
print("Changed "..v.Name.."'s collision group to Ghosts")
PhysicsService:SetPartCollisionGroup(v, "Ghosts")
end
v.Transparency = 0.5
player.PlayerGui.GhostGui.ImageLabel.ImageTransparency = 0
else
if v:IsA("BasePart") then
print("Changed "..v.Name.."'s collision group to Humans")
PhysicsService:SetPartCollisionGroup(v, "Humans")
end
v.Transparency = 0
player.PlayerGui.GhostGui.ImageLabel.ImageTransparency = 1
end
end
end
if v:IsA("BasePart") then
if v.Transparency == 0 then
print("Changed "..v.Name.."'s collision group to Ghosts")
PhysicsService:SetPartCollisionGroup(v, "Ghosts")
else
print("Changed "..v.Name.."'s collision group to Humans")
PhysicsService:SetPartCollisionGroup(v, "Humans")
end
end
end
end
remoteEvent.OnServerEvent:Connect(ghostPlayer)