So I made this script, where if you type in a ID from the decal catalog, it will insert it into your swords TextureID, and here's the problem it's getting annoying because everytime someone dies they have to reinsert it. Could someone edit it and fix it so it saves when a player changes it?
01 | repeat wait() until game.Players.LocalPlayer |
02 |
03 | local player = Game.Players.LocalPlayer |
04 |
05 | local swordMesh = script.Parent |
06 |
07 | local GuiBar = player.PlayerGui.ScreenGui.Frame.TextBox --Change to correct path. |
08 |
09 | GuiBar.FocusLost:connect( function () --When someone is finished typing into the TextBox, |
10 |
11 | pcall ( function () |
12 |
13 | swordMesh.TextureId = "http://www.roblox.com/asset/?id=" .. tostring ( tonumber (GuiBar.Text)- 1 ) --Try to change the texture. |
14 |
15 | end ) |
16 | end ) |
I'd store a StringValue in the Player for the current texture they have.
01 | while not Game.Players.LocalPlayer do wait( 0 ) end |
02 | local Player = Game.Players.LocalPlayer |
03 | local SavedTexture = Player:FindFirstChild( "SavedTexture" ) |
04 | local Mesh = script.Parent |
05 | if not SavedTexture then |
06 | SavedTexture = Instance.new( "StringValue" ) |
07 | SavedTexture.Name = "SavedTexture" |
08 | SavedTexture.Value = Mesh.TextureId |
09 | SavedTexture.Parent = Player |
10 | end |
11 |
12 | local Input = Player.PlayerGui.ScreenGui.Frame.TextBox -- Try accessing that starting from this script? |
13 | Input.FocusLost:connect( function () |
14 | -- If 'Input.Text' is able to be transformed into a number, subtract 1 from it, or default to 0 if it cannot be transformed into a number |
15 | local id = tonumber (Input.Text) and tonumber (Input.Text) - 1 or 0 |
16 | Mesh.TextureId = "http://www.roblox.com/asset/?id=" .. id |
17 | SavedTexture.Value = Mesh.TextureId |
18 | end ) |
19 | Mesh.TextureId = SavedTexture.Value -- Load previous texture |
ASSUMING YOU HAVE A LOCAL SCRIPT!
You can use DataPersistance to store instances, save them with this method:
1 | player:SaveInstance( "SwordTextureId" , swordMesh.TextureId) |
And then load them with this method:
1 | swordMesh.TextureId = player:LoadInstance( "SwordTextureId" ) |
Much easier is to create 2 functions, saveData(player) and call it when a player dies, and the last one is loadData(player), call it when the player respawns
01 | function saveData(player) |
02 | player:SaveInstance( "SwordTextureId" , swordMesh.TextureId) |
03 | end |
04 |
05 | function loadData(player) |
06 | swordMesh.TextureId = player:LoadInstance( "SwordTextureId" ) |
07 | end |
08 |
09 | if game.Players.LocalPlayer.Character.Humanoid.Health < = 0 then |
10 | saveData(game.Players.LocalPlayer) |
11 | end |
12 |
13 | game.Players.LocalPlayer.CharacterAdded:connect( function (character) |
14 | loadData(game.Players.LocalPlayer) |
15 | end ) |
But this condition:
1 | if game.Players.LocalPlayer.Character.Humanoid.Health < = 0 then |
2 | saveData(game.Players.LocalPlayer) |
3 | end |
will not work, why? Because it isn't called! We need to make a check function like this:
1 | function onUpdate() |
2 | if game.Players.LocalPlayer.Character.Humanoid.Health < = 0 then |
3 | saveData(game.Players.LocalPlayer) |
4 | end |
5 | wait( 0.5 ) -- Wait 0.5 seconds before calling again the function |
6 | onUpdate() -- We call again the function |
7 | end |
At the end of the localscript call the function onUpdate()
1 | -- Code |
2 | onUpdate() -- We call the onUpdate for the first time |