Okay so i have this script and with some pretty simple code right, WRONG, it won't work when someone joins/respawns. No output telling why, just like- Help please?
--Script game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Wait(function(char) for i = 1,20 do local text = plr:WaitForChild('PlayerGui'):FindFirstChild('ScreenThings'):FindFirstChild('ShowingMoneyFrame'):FindFirstChild('HonksText') text.Text = 'Honks: '.. plr:FindFirstChild('leaderstats'):FindFirstChild('Honks').Value wait(1) end end) end)
Excuse my ignorance on the matter, as I haven't actually done anything in Studio for a good year. I can't remember if the server can make changes to the PlayerGui, although I thought they could. As such, I'll provide a server and a local option.
Ziffxture was correct, you should not be using .CharacterAdded:Wait()
. Instead, you should simply connect it to a function, as you did with .PlayerAdded
.
Server
game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(char) for i = 1,20 do local text = plr:WaitForChild('PlayerGui'):FindFirstChild('ScreenThings'):FindFirstChild('ShowingMoneyFrame'):FindFirstChild('HonksText') text.Text = 'Honks: '.. plr:FindFirstChild('leaderstats'):FindFirstChild('Honks').Value wait(1) end end) end)
Local
local Players = game:GetService("Players") local Player = Players.LocalPlayer Player.CharacterAdded:Connect(function(Character) for i = 1,20 do local text = Player:WaitForChild('PlayerGui'):FindFirstChild('ScreenThings'):FindFirstChild('ShowingMoneyFrame'):FindFirstChild('HonksText') text.Text = 'Honks: '.. Player:FindFirstChild('leaderstats'):FindFirstChild('Honks').Value wait(1) end end)
So there are two options, keeping in mind that the LocalScript will have to be placed appropriately.
I also do not recommend using so many :FindFirstChild()
s. In the first place, their usage in your case is completely irrelevant. The point of using the method is to make sure an instance exists before you attempt to interact with it, thus avoiding the dreaded attempt to index a nil value
error. However, in your line of code immediately after declaring the variable text
, you're trying to changing the Text
property of the instance. This means that if, for some reason, the instance did not exist, you would be indexing a nil value, nullifying the purpose of your usage of :FindFirstChild()
. To solve this, or rather make it more helpful, use a simple if
statement.
local Object = workspace:FindFirstChild("Object") if Object then Object.Name = "Found!" end