Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Saving the texture can someone help?

Asked by 11 years ago

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?

01repeat wait() until game.Players.LocalPlayer
02 
03local player = Game.Players.LocalPlayer
04 
05local swordMesh = script.Parent
06 
07local GuiBar = player.PlayerGui.ScreenGui.Frame.TextBox --Change to correct path.
08 
09GuiBar.FocusLost:connect(function() --When someone is finished typing into the TextBox,
10 
11pcall(function()
12 
13swordMesh.TextureId = "http://www.roblox.com/asset/?id="..tostring(tonumber(GuiBar.Text)-1) --Try to change the texture.
14 
15end)
16end)
0
The information is showing you what the stuff does, so you can figure it out. Wrongmistake 0 — 11y

2 answers

Log in to vote
0
Answered by
Ekkoh 635 Moderation Voter
11 years ago

I'd store a StringValue in the Player for the current texture they have.

01while not Game.Players.LocalPlayer do wait(0) end
02local Player = Game.Players.LocalPlayer
03local SavedTexture = Player:FindFirstChild("SavedTexture")
04local Mesh = script.Parent
05if not SavedTexture then
06    SavedTexture = Instance.new("StringValue")
07    SavedTexture.Name = "SavedTexture"
08    SavedTexture.Value = Mesh.TextureId
09    SavedTexture.Parent = Player
10end
11 
12local Input = Player.PlayerGui.ScreenGui.Frame.TextBox -- Try accessing that starting from this script?
13Input.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
18end)
19Mesh.TextureId = SavedTexture.Value -- Load previous texture
0
Didn't work. Wrongmistake 0 — 11y
0
Output? Ekkoh 635 — 11y
Ad
Log in to vote
0
Answered by 11 years ago

ASSUMING YOU HAVE A LOCAL SCRIPT!

You can use DataPersistance to store instances, save them with this method:

1player:SaveInstance("SwordTextureId", swordMesh.TextureId)

And then load them with this method:

1swordMesh.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

01function saveData(player)
02    player:SaveInstance("SwordTextureId", swordMesh.TextureId)
03end
04 
05function loadData(player)
06    swordMesh.TextureId = player:LoadInstance("SwordTextureId")
07end
08 
09if game.Players.LocalPlayer.Character.Humanoid.Health <= 0 then
10    saveData(game.Players.LocalPlayer)
11end
12 
13game.Players.LocalPlayer.CharacterAdded:connect(function(character)
14    loadData(game.Players.LocalPlayer)
15end)

But this condition:

1if game.Players.LocalPlayer.Character.Humanoid.Health <= 0 then
2    saveData(game.Players.LocalPlayer)
3end

will not work, why? Because it isn't called! We need to make a check function like this:

1function 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
7end

At the end of the localscript call the function onUpdate()

1-- Code
2onUpdate() -- We call the onUpdate for the first time

Answer this question