Hello, can someone help me with this?
This script is to change a body color when the player is added to the game. The script works fine for the first time but when you change the colorvalue in playerstats, the body color doesnt change to that color.
Example:
player has red torso and pushes a button to change to green torso (that button also changes playerstats.color to green).
when the player dies, the color in playerstats.color stays lime green but their torso color changes back to red.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
print("player loaded")
local color = player.playerstats.Color
print(color.Value)
character["Body Colors"].TorsoColor = color.Value
print(color.Value)
print("color loaded")
end)
end)
-- ignore all the prints statements, that was just to help myself :)
The color variable in the CharacterAdded event updates every time the character respawns, which gets the updated player stats color and uses it on the torso. If that’s the case, then maybe put the color variable outside of the CharacterAdded and inside of the PlayerAdded; but make sure to use :WaitForChild() to wait for playerstats.
game.Players.PlayerAdded:Connect(function(player)
local color = player:WaitForChild("playerstats").Color
player.CharacterAdded:Connect(function(character)
print("player loaded")
print(color.Value)
character["Body Colors"].TorsoColor = color.Value
print(color.Value)
print("color loaded")
end)
end)
-- ignore all the prints statements, that was just to help myself :)
That doesn’t fix anything, the same things happen, sorry
For example 1, how does the player get the red torso? Do they get it by joining the game, and respawning?
If they get it by joining the game, does it vary in their player stats? Like, change the torso color every time they join because of their new player stats.
it’s like when they join the game their standart color is red (so yeah by joining) and they just can change body colors when pressed a button.
that part works fine but the problem is when they die, the body colors reset back to red and not the color that they have choosen.
so red torso is standart torso every time they join and that stays so, they can only change body color when pressing a button and if they leave, the body colors reset back to red if they join again (basicly it’s not saving)
So the problem is the torso color resetting back to red upon dying. Now I understand.
See if this works.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
print("player loaded")
local color = player.playerstats.Color
print(color.Value)
character:FindFirstChildWhichIsA("BodyColors").TorsoColor = color.Value
print(color.Value)
print("color loaded")
end)
end)
-- ignore all the prints statements, that was just to help myself
If that doesn’t work, then try this instead.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
print("player loaded")
print(player.playerstats.Color.Value)
character:FindFirstChildWhichIsA("BodyColors").TorsoColor = player.playerstats.Color.Value
print(player.playerstats.Color.Value)
print("color loaded")
end)
end)
-- ignore all the prints statements, that was just to help myself
If none of them work, then try what blueberry suggested.
game.Players.PlayerAdded:Connect(function(player)
local playerstats = Instance.new("Folder")
playerstats.Name = "playerstats"
playerstats.Parent = player
local color = Instance.new("StringValue")
color.Name = "Color"
color.Value = "Really red"
color.Parent = playerstats
player.CharacterAdded:Connect(function(character)
print("player loaded")
local color = player:WaitForChild("playerstats"):WaitForChild("Color")
print(color.Value)
character:WaitForChild("Body Colors").TorsoColor = BrickColor.new(color.Value)
print(color.Value)
print("color loaded")
end)
end)
I don’t know what kind of button you use and what script, but this is working for me by doing this:
A LocalScript inside the textbutton on the screen to press to change color. Then use a RemoteEvent to send to a server script to change the color and change the value of the color in playerstats.
LocalScript:
local remote = script.Parent:WaitForChild("RemoteEvent")
script.Parent.MouseButton1Click:Connect(function()
remote:FireServer("Lime green")
end)
Script:
local remote = script.Parent:WaitForChild("RemoteEvent")
remote.OnServerEvent:Connect(function(player,colorname) -- player is always the first parameter passed through the remote onserver, colorname is any variable following that came through
local oldcolor = player.playerstats.Color
oldcolor.Value = colorname
player.Character["Body Colors"].TorsoColor = BrickColor.new(colorname)
end)
ETA: If you are using only LocalScript to do everything without using a RemoteEvent or regular server script to change the value in playerstats.Color, then the server won’t even accept that and that’s why player will join with the default red color again.
Also, to give a new color to a BasePart, do part.BrickColor = BrickColor.New(“Lime green”) or the color variable name, but put inside BrickColor.new(), that’s why the way to write this is: player.Character["Body Colors"].TorsoColor = BrickColor.new(colorname) or character:WaitForChild("Body Colors").TorsoColor = BrickColor.new(color.Value)
ETA2: I see that you say the color is a BrickValue; I’m assuming it’s a BrickValue object and not a StringValue. If that’s so, using the method I gave plus the brickvalue inside playerstats, the server script will be like:
local remote = script.Parent:WaitForChild("RemoteEvent")
remote.OnServerEvent:Connect(function(player,colorname)
local oldcolor = player.playerstats.Color
oldcolor.Value = BrickColor.new(colorname)
player.Character["Body Colors"].TorsoColor = BrickColor.new(colorname)
end)