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

How can I make this local script in StarterPlayerScripts detect change midlife?

Asked by 6 years ago
Edited 6 years ago

In a game I am making, I have hazards that players have to work together to cross based on what team they are on. I use the team system to allow immunity to certain teams from specific hazards so that players work together to tackle obstacles. I wanted one team to be able to walk over gaps to access things that other players can't by using a local block that would only have CanCollide set as true for that one team, I used a local script inside of the StarterPack that would do all of this, however, it will only work if players spawn as the team that walks over the gap, and I have points where players can switch teams to add more depth to puzzles meaning that the players who should be able to pass gaps would instead fall. I had tried using "repeat" to make the script continuously loop allowing players to change teams in the same life and still gain the effects of the new team instead of having to respawn as the new team and loose progress, but it did not seem to work, after fiddling with the script, searching free models for something that might help and looking up tutorials, I had finally come here for help, any help on this would be greatly appreciated, here is the script

local part = Instance.new("Part", workspace) --Part is inside of workspace
part.BrickColor = BrickColor.new(25) --Change all the "..."s to any value you want!
part.Size = Vector3.new(526, 0.2, 605.58)
part.CFrame = CFrame.new(1867.82, 27.621, -95.245) --The position
part.Name = "Fly"
part.Anchored = true
part.Transparency = .5
local player = game:GetService("Players").LocalPlayer --The player

if player.TeamColor == BrickColor.new("Bright red") 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 = true --Let them walk through
else --If they're any other color then... You can do "elseif" to add another if then statement.
    part.CanCollide = false --Block the enemy players!
end

1 answer

Log in to vote
0
Answered by 6 years ago

Your best bet is to fire the function when a property of the player changes. You should try to avoid loops as much as possible as they can create lag.

In this script, it will fire the function when the script exists, and it will fire it again whenever the player has had a change in properties (TeamColor is one of those properties).

local part = Instance.new("Part", workspace) --Part is inside of workspace
part.BrickColor = BrickColor.new(25) --Change all the "..."s to any value you want!
part.Size = Vector3.new(526, 0.2, 605.58)
part.CFrame = CFrame.new(1867.82, 27.621, -95.245) --The position
part.Name = "Fly"
part.Anchored = true
part.Transparency = .5
local player = game:GetService("Players").LocalPlayer --The player

function TeamChange()
    if player.TeamColor == BrickColor.new("Bright red") 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 = true --Let them walk through
    else --If they're any other color then... You can do "elseif" to add another if then statement.
        part.CanCollide = false --Block the enemy players!
    end
end

TeamChange() --Fire the TeamChange function

player.Changed:connect(function()
    print(player.TeamColor) --What's the player's TeamColor?
    TeamChange() --Fire the TeamChange function
end)

Click Accept Answer if this was what you're looking for! Comment if you have any different concerns!

Ad

Answer this question