Hi, I have a script that should enable a screenGui in the playerGui when some conditions are met. However sometimes it works and sometimes it doesn’t. A few things to note are:
The players die shortly before the gui is enabled (resetOnspawn is false)
When I go on the server it says its enabled for that players playerGui but then when I look on the clients playerGui it says its not.
Sometimes it works perfectly which is weird
If you have any ideas please let me know, I can share the code but its very long and hard to explain, but if you need it let me know.
Are you sure? I’ve got it everywhere in my code and it works perfectly. Here for example, when a player clicks a button it fires the server and this code is ran in a serverscript.
donatePlayer1.OnServerEvent:Connect(function(player)
if player == losers[1] or player == losers[2] or player == losers[3] then
player.PlayerGui.DonationPage.Enabled = false
player.PlayerGui.Donation1.Enabled = true
end
end)
donatePlayer2.OnServerEvent:Connect(function(player)
if player == losers[1] or player == losers[2] or player == losers[3] then
player.PlayerGui.DonationPage.Enabled = false
player.PlayerGui.Donation2.Enabled = true
end
end)
donatePlayer3.OnServerEvent:Connect(function(player)
if player == losers[1] or player == losers[2] or player == losers[3] then
player.PlayerGui.DonationPage.Enabled = false
player.PlayerGui.Donation3.Enabled = true
end
end)
I wouldn’t at all change all that stuff on the server, reason being that if any element is missing due to the player leaving mid-way then the server will error, but if you send a remote event to the client, and the player leaving, no error will occur.
The odds of a player leaving mid-way is close to non-existent, but there is no reason to create such scenarios.
Also your Async can fail, so make sure to wrap them into pcalls.
You should only try to call for the player thumbnail once, whenever they join the game. You then store those thumbnails in a table on the server. When the player leaves the game, you then delete their stored thumbnail from the table. Make sure to have a backup thumbnail to use, for whenever the thumbnail-pcall fails
Here you go:
local backup_thumbnail = "image_string_url"
local player_thumbnails = {}
players.PlayerAdded:Connect(function(player)
-- Store thumbnail from player that joined
local content, isReady = pcall(function()
return players:GetUserThumbnailAsync(player.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size150x150)
end)
player_thumbnails[tostring(player.UserId)] = (isReady and content) or backup_thumbnail
end)
players.PlayerRemoving:Connect(function(player)
-- Remove stored thumbnail from player that left
if player_thumbnails[tostring(player.UserId)] then
player_thumbnails[tostring(player.UserId)] = nil
end
end)
Sorry if any code-spelling error is in there, I forked bits of it from one of my games.
If you want the thumbnails to be accessible by the server from multiple scripts, then create a module script with an dictionary of data, where player_thumbnails could be one of the data in there
local module = {
minimum_something = 3,
players_to_something = 10,
player_thumbnails = {},-- this way you can fetch the player's thumbnail from the server, in any server script
}
local data_module = require(game.ServerStorage.Data_Module) -- this should be outside any loop/event
local thumbnail = data_module.player_thumbnails[tostring(player.UserId)]
-- We already know if the player is in the game, then they have a thumbnail.
--No matter if it's their real thumbnail or placeholder thumbnail.
--So no need to check
Thank you but I have already made a way to move the list to other scripts. What I was wondering is how i would say get “player1”'s thumbnail from the list?
If you know the player, then you know their userId. thus it’s simple just doing
local get_thumbnail = player_thumbnails[tostring(player.UserId)] -- this will give you the thumbnail
In practice it tries to fetch a index in the dictionary, called whatever the userid of the player is.
If no such index is present, then it will return nil.
If you want to make it 100% bulletproof, then check if get_thumbnail is fetched by doing:
local get_thumbnail = player_thumbnails[tostring(player.UserId)] -- this will give you the thumbnail
if get_thumbnail then
-- do something with thumbnail
end