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

How can I make a block that holds one team and not the other(s)?

Asked by 8 years ago

I have been working on a game and I wanted a part where players on one team can walk over a block while players on another team fall through it. I was going to make it so that if a player from the team I want to be able to stand touched it the script would set "can collide" to true and when they weren't on it "can collide" would be set to off. However this would mean that if that player was on the block then the other team would be able to cross it until they got off. this would create problems and make things too easy. I don't even know if it is possible to even make a script like that. I tried plugging a few promising script parts into the end of this one:

script.Parent.Touched:connect(function(hit)

local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

if Player and Player.TeamColor == BrickColor.new("team color") then

(script end) to try and get the results I wanted to no avail. I had even tried the free models section. Now i'm at my ropes end, so I came here to ask for help.

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

this script will make Pearl team color go through. while black team color it turns it back so they cant


script.Parent.Touched:connect(function(hit) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player and Player.TeamColor == BrickColor.new("Pearl") then script.Parent.CanCollide = false elseif Player and Player.TeamColor == BrickColor.new("Black") then script.Parent.CanCollide = true end end)
0
What if 2 people on both teams go in at the same time. The script won't work then because both of them can't go through! EzraNehemiah_TF2 3552 — 8y
0
If some random block that isn't the player touches it then it won't work either! hit.Parent if it's a part in workspace will give you the workspace datamodel, which isn't a character and will error. EzraNehemiah_TF2 3552 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Better solution

Local parts are parts that are only client sided, meaning that the server can't interact with it and everyone else cannot see it.


Local parts

To make local parts you just need to turn on Filtering Enabled which is a property of the workspace datamodel. This will stop client-sided code from changing the server. So if we turn FE on and insert a part into workspace, only the player with that local script is able to see it.

--Local Script
if game:GetService("Players").LocalPlayer.Name == "Player1" then
    local part = Instance.new("Part",workspace) --Adds a part to workspace
end

if workspace:FindFirstChild("Part") then
    print("Part exists") --Print if the part is findable
else
    print("Part doesn't exist") --Print if the part is nonexistent.
end

Part exists

--Every server-sided script and other player's local script
if workspace:FindFirstChild("Part") then
    print("Part exists") --Print if the part is findable
else
    print("Part doesn't exist") --Print if the part is nonexistent.
end

Part doesn't exist

So in this experiment, only the person named "Player1" can see that part. Everyone else and all the server scripts cannot, because the part only exists on that computer and that computer only.


Final Product

Local Script. The way I do it, you don't need to worry about GetPlayerFromCharacter erroring if a rogue part accidently touches it and either blocks all the players or lets all the players in. If you do it all server-sided, you can actually sneak passed the brick as long as someone else on the other team touches it after you touch it. Basically it's error free! MAKE SURE FILTERING ENABLED IS... well, enabled.

local part = Instance.new("Part", workspace) --Part is inside of workspace
part.BrickColor = BrickColor.new(...) --Change all the "..."s to any value you want!
part.Size = Vector3.new(...)
part.CFrame = CFrame.new(...) --The position
part.Name = ...
local player = game:GetService("Players").LocalPlayer --The player

if player.TeamColor == BrickColor.new("Bright red") or player.TeamColor == BrickColor.Green() then --If they're a bright red color OR if they're BrickColor.Green (Bright green) then... (you can add more "or"s)
    part.CanCollide = false --Let them walk through
else --If they're any other color then... You can do "elseif" to add another if then statement.
    part.CanCollide = true --Block the enemy players!
end

If you put this inside of starter player scripts then it'll make a new part for everyone so everyone can see the part, only some people can pass through.



Hope it helps!

Answer this question