so basically this is a stat script for a basketball game or whatever. So basically when a player joins a game its supposed to clone this frame and change the textlabel text to the players Name. That works fine but when a new player joins, on the person who joined the server previously it just clones it and doesnt change the name. For the new player it just says their name only (sorry im bad at explaining things)
-- Server Script -- game.Players.PlayerAdded:Connect(function(player) local NewFolder = Instance.new("Folder") NewFolder.Name = player.Name NewFolder.Parent = game.Workspace:WaitForChild("Stat") local PointsValue = Instance.new("NumberValue") PointsValue.Value = 0 PointsValue.Parent = NewFolder PointsValue.Name = "Points" local AssistsValue = Instance.new("NumberValue") AssistsValue.Value = 0 AssistsValue.Parent = NewFolder AssistsValue.Name = "Assists" local ReboundsValue = Instance.new("NumberValue") ReboundsValue.Value = 0 ReboundsValue.Parent = NewFolder ReboundsValue.Name = "Rebounds" local StealsValue = Instance.new("NumberValue") StealsValue.Value = 0 StealsValue.Parent = NewFolder StealsValue.Name = "Steals" local BlockValue = Instance.new("NumberValue") BlockValue.Value = 0 BlockValue.Parent = NewFolder BlockValue.Name = "Blocks" game.ReplicatedStorage:WaitForChild("NameStat"):FireAllClients() end)
-
-- Local Script -- local replicated = game:GetService("ReplicatedStorage") replicated:WaitForChild("NameStat").OnClientEvent:Connect(function() local frame = script.Parent.StatsClone.PlayerStats local frameclone = frame:Clone() frameclone.Parent = script.Parent.Home frameclone.Visible = true frameclone:WaitForChild("PlayerName").Text = game.Players.LocalPlayer.Name end)
If you create a new instance using Instance.new()
, you should parent it after setting all of the properties for it because of performance reasons. It's also why you shouldn't use the second parameter to Instance.new()
. You can read more about it on this post
When you either Clone() or Instance.new(), it should look like this:
--example on line 8 of the server script: local PointsValue = Instance.new("NumberValue") PointsValue.Name = "Points" PointsValue.Value = 0 PointsValue.Parent = NewFolder
I'm not following your explanation that you gave for the problem since it is difficult to understand, but just by looking at the logic of the code does explain it.
I believe you want all clients to insert a new TextLabel with it's name set to the joining player. In your local script code, you set the text of the label to the LocalPlayer in the OnClientEvent
. If you do this, it would clone a new label with the name set to the client it's under.
What you should do is pass the information you want all clients to know (Which is the player's name that is joining) through the :FireAllClients()
argument. That is what RemoteEvents are intended for after all.
--server script local replicatedStorage = game:GetService("ReplicatedStorage") local nameStateEvent = replicatedStorage.NameStat --PlayerAdded has the player instance as the parameter! game.Players.PlayerAdded:Connect(function(player) --previous code --Get the name of the player and send it to all clients. nameStatEvent:FireAllClients(player.Name) end)
In the OnServerEvent
of the local script, the first parameter is the data that we had passed from the server. We set this to the Text of the Textlabel.
--Make better names for variables. Helps a lot when you add more code to your scripts. local replicatedStorage = game:GetService("ReplicatedStorage") --Know that we are passing a string value! --I called the parameter 'name' replicatedStorage:WaitForChild("NameStat").OnClientEvent:Connect(function(name) --This variable pathing should be cleaner local frame = script.Parent.StatsClone.PlayerStats --Follow the Parent rule I stated at the top local frameclone = frame:Clone() frameclone.PlayerName.Text = name --Set Text to name which is a string frameclone.Visible = true frameclone.Parent = script.Parent.Home --Make sure this is right end)
Let me know if this explained what's going on and if it was what you were looking for.