I made a tool that clones a part from ReplicatedStorage to the Workspace when used, and some players can collide with it, while others don’t.
What I wanted to do is the part to be transparent or fully visible on the client depending on if the local player can collide with it or not.
I don’t have a clear idea of how I could execute that, especially since it’s cloned and there could be various instances of it at once.
The players that can collide with it have an IntValue as a child, so I suppose that could help greatly in doing this.
Pardon me if the post wasn’t very clear.
So if you already have the collision scripted, I don’t see why you can’t have the transparency be changed the same way
I used Collision Groups, which so far works nicely.
You need to process all such parts when the player joins and whenever one of them is created.
Assuming all the parts are in a model:
The first one is trivial, you can just loop through all of the model’s children.
The second one can be done with the .ChildAdded
event.
local model = workspace.Parts
local function onPartAdded(part)
part.Transparency = 2/3
end
model.ChildAdded:Connect(onPartAdded)
for _,part in ipairs(model:GetChildren()) do
onPartAdded(part)
end
edit: how convenient, you could make it so only players who can pass through these parts have this script
You can also use a remote event to announce new parts — from the server, broadcast to all players; on the client, make these parts transparent if they should be — but it’s much easier to just stay within one tidy Model.
edit: If players can switch back to colliding with the parts, you will also need to loop through all the parts and make them visible again. You’ll also need some sort of signal (a Remote Event, or a BoolValue and its .Changed
event) to make the script start/stop processing new parts.
You can make the ChildAdded
connection stop working by either making onPartAdded
check whether the player should collide with the part, or by putting the connection in a variable and disconnecting it.
actually,
local player = game:GetService("Players").LocalPlayer
local model = workspace.Parts
local connection
-- enabled as in collision is enabled
local function partEnable(part)
part.Transparency = 0
end
local function partDisable(part)
part.Transparency = 2/3
end
local function collisionDisable()
if connection then connection:Disconnect()
connection = model.ChildAdded:Connect(partDisable)
for _,part in ipairs(model:GetChildren()) do
partDisable(part)
end
end
local function collisionEnable()
if connection then connection:Disconnect()
-- connection = model.ChildAdded:Connect(partEnable)
for _,part in ipairs(model:GetChildren()) do
partEnable(part)
end
end
if player:FindFirstChild("IntValue") then -- adjust if your IntValue is not named this way
collisionDisable()
else
collisionEnable()
end
-- checking player.ChildAdded and player.ChildRemoving is left as an exercise for you at the moment
-- I think you really ought to make it so the collision state is in a BoolValue that always exists instead of an IntValue that might exist
-- that way, you can do...
--[[
state = player:WaitForChild("BoolValue")
function collisionUpdate(newstate)
if newstate then
collisionDisable()
else
collisionEnable()
end
end
state.Changed:Connect(collisionUpdate)
collisionUpdate(state.Value)
]]