I’ve tried to make this script work and couldn’t understand where I had gone wrong. I’m trying to make this script change the player’s team and reset them. I’m not the best scripter so I had to resort to the forum.
local player = script.Parent.Parent
script.Parent.MouseButton1Click:Connect(function()
player.Team = game.Teams:FindFirstChild("Police")
player.character.head:destroy()
end)
This script doesn’t reset the player or change the player’s team, can someone tell me how to fix it?
I recommend you to use RemoteEvents and fire them when someone changes the team, your script looks like a 2012 code, try using modern Roblox physics instead.
Local Script: (Inside the button or anywhere)
-- Gets the player
local LocalPlayer = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
-- I think the button has the script inside, you can change this.
local Button = script.Parent
-- As a example, Really Red is the current team color (You can change)
local SelectedTeamColor = BrickColor.new("Really Red")
-- You will need to use a RemoteEvent to fire server when someone changes the team.
local Event = game.ReplicatedStorage:WaitForChild("TeamEvent")
Button.MouseButton1Click:Connect(function()
Event:FireServer(BrickColor.new(SelectedTeamColor) -- You can use BrickColor.new().
end)
Script: (I recommend in ServerScriptService)
local Event = game.ReplicatedStorage:WaitForChild("TeamEvent")
Event.OnServerEvent:Connect(function(Player,ColorOfTeam)
Player.TeamColor = TeamColor
Player:LoadCharacter() -- You can use TakeDamage if you want to kill player!
end)
Thank you, I also have a little trouble with this script, like I said, I’m not a good scripter. I’m mainly a builder. Can you please tell me what is making this script not function?
Hey, I think this should be in the Scripting Support category.
You don’t have to keep using Parent to find the player, try this instead:
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
player.Team = game.Teams:FindFirstChild("Police")
player.Character.Head:destroy()
end)
I am assuming you want to change someone’s team and respawn them so instead you can just use remote events. Fire the server from that local script code and then the server can instead change the team and kill the character in whatever way you want. I recommend just using :LoadCharacter() on the player instead of removing their head.
This is what it’ll look like (place remote event named TeamSwitchEvent in ReplicatedStorage):
Local script:
local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- this is your remote event
script.Parent.MouseButton1Click:Connect(function()
remoteEvent:FireServer()
end)
Server script:
local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- same remote event
local policeTeam = game.Teams.Police
remoteEvent.OnServerEvent:Connect(function(player) -- player gets passed as a parameter
player.Team = policeTeam -- set their new team
player:LoadCharacter() -- respawn the character
end)
The player could also spam the button to respawn themselves if they’re already police so you could use an if statement on the server code like this:
local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- same remote event
local policeTeam = game.Teams.Police
remoteEvent.OnServerEvent:Connect(function(player) -- player gets passed as a parameter
if player.Team ~= policeTeam then
player.Team = policeTeam -- set their new team
player:LoadCharacter() -- respawn the character
end
end)
local remoteEvent = game.ReplicatedStorage.TeamSwitchEvent -- same remote event
local policeTeam = game.Teams.Police
remoteEvent.OnServerEvent:Connect(function(player) -- player gets passed as a parameter
if player.Team == policeTeam then return end -- stop the function if the player is already in the police side.
player.Team = policeTeam -- set their new team
player:LoadCharacter() -- respawn the character
end)