I have a value in workspace, it’s value is the name of a player.
How would I go about changing that players team.
e.g I want to change santascool99’s team.
I have a value in workspace, it’s value is the name of a player.
How would I go about changing that players team.
e.g I want to change santascool99’s team.
You could do:
local value = game.Workspace.PlayerName
local player = game.Players:FindFirstChild(value.Value)
local newteam = game.Teams.SomeTeam
player.Team = newteam
Make sure to change value to the actual value, and newteam to the actual team.
local value = --Value Location
game:GetService("Players"):FindFirstChild(value.Value).Team = --Team here.
You just FindFirstChild on players service with a player’s name.
local Value = ...
local Player = game:GetService("Players"):FindFirstChild(Value.Value)
Player.Team = ...
edit: what mistrustfully said
From what it sounds like, his value is just the string name of the player, not some obj reference. So you’d be trying to manipulate a non-existant .Team property of said string.
Edit: @liuthemoo Hey, I saw you changed your code (presumably after seeing my comment). I just want to let you know that the devforum is a place to learn! Even if you post incorrect code, someone will (hopefully) point it out and future developers reading these threads may learn something from your mistake and from the person correcting you. There’s absolutely nothing wrong with making mistakes!
If you a StringValue
stored in Workspace, you can select the player from Players and change their team by doing
game:GetService("Players"):FindFirstChild(value.Value).Team = game:GetService('Teams'):FindFirstChild("Blue")
There’s a better way to do this rather that would shorten it down.
@liuthemoo’s solution WOULD WORK (no idea why you changed it) if you used the alternative.
Instead of assigning the player’s name to a StringValue
, you can use an ObjectValue
. You would do something along the lines of ObjectValue.Value = game:GetService('Players').Player
. When you do this, you would change the Player’s Team by doing Player.Value.Team = newTeam
.
edit: using GetService
More on the topic of ‘ObjectValues’ I set the value to the player (in players)
In a different script, how would I change the players team.
You would access the same ObjectValue
from that Script. If you store the ObjectValue
in, let’s say, ServerStorage
, you can access it like you would access any other object: game:GetService("ServerStorage").Object.Value
. When you access it like this, you’re essentially looking at the Player. If you were to do .Team following that, you would then have access to the Player’s Team object.
so game:GetService(‘ServerStorage’).Object.Value.Team ?
That would return the Team the player is on, assuming that you stored the ObjectValue in ServerStorage and called it Object
. If you want to change the Team they’re on, you can just append = game:GetService("Teams"):FindFirstChild("INSERT_TEAM_NAME_HERE")
to the end.
Oops, probably should’ve replied to you acknowledging having read your comment. I completely understand that incorrect code is perfectly acceptable, but the last thing I’d want is for a developer to get the idea that Object Values and String Values are the same or something. Thanks for pointing out my error!
While this does make perfect sense, it isn’t working, neither is it giving an error
Can you show your hierarchy and your code?
Here is where I am trying to work my way towards.
Blue.Touched:Connect(function(hit)
if hit.Name == 'Thing' then
print('Touched') -- It prints this and everything else below doesn't happen.
hit.Owner.At.Value.Team = game:GetService('Teams'):FindFirstChild('Blue')
end
end)
Why do you have two Value objects? You should make sure that that the object you’re using is an ObjectValue
and not a StringValue
and has a value assigned to it. Is there a Team called Blue
?
The At Value is a ObjectValue, and is assigned to the player. Also yes there is a team called blue, hence why I am confused.
How are you assigning the Player to the ObjectValue? Can you show the hierarchy in Teams?
The At is changed by a remote event fired by the player.
local RS = game:GetService(“ReplicatedStorage”)
local Event = RS:WaitForChild(“ChangeOwner”)
Event.OnServerEvent:connect(function(player, Thing)
Thing:SetNetworkOwner(player)
Thing.Owner.At.Value = player
end)
It changes the ‘At’ Value to the player, the only problem is the team not changing.
You’re saying that you’re getting no errors. This issue you’re having should not be happening. Are you sure both your Touched event connection and RemoteEvent (be sure that it’s an Event and not Function) are working? I replicated your code and hierarchy and it works fine for me. The problem you’re having is due to outside interference from either your set up or another script.
P.S: Your code isn’t formatted properly. Please look at this topic I posted to see how you can format it. You’re using block quotes and not code blocks.
Hm, I am not sure as to why this isn’t working, but I will test in a new server and see how it goes.
I will also use code blocks, thanks for that.