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 10 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?

repeat wait() until game.Players.LocalPlayer

local player = Game.Players.LocalPlayer

local swordMesh = script.Parent

local GuiBar = player.PlayerGui.ScreenGui.Frame.TextBox --Change to correct path.

GuiBar.FocusLost:connect(function() --When someone is finished typing into the TextBox,

pcall(function()

swordMesh.TextureId = "http://www.roblox.com/asset/?id="..tostring(tonumber(GuiBar.Text)-1) --Try to change the texture.

end)
end)
0
The information is showing you what the stuff does, so you can figure it out. Wrongmistake 0 — 10y

2 answers

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

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

while not Game.Players.LocalPlayer do wait(0) end
local Player = Game.Players.LocalPlayer
local SavedTexture = Player:FindFirstChild("SavedTexture")
local Mesh = script.Parent
if not SavedTexture then
    SavedTexture = Instance.new("StringValue")
    SavedTexture.Name = "SavedTexture"
    SavedTexture.Value = Mesh.TextureId
    SavedTexture.Parent = Player
end

local Input = Player.PlayerGui.ScreenGui.Frame.TextBox -- Try accessing that starting from this script?
Input.FocusLost:connect(function()
    -- 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
    local id = tonumber(Input.Text) and tonumber(Input.Text) - 1 or 0
    Mesh.TextureId = "http://www.roblox.com/asset/?id=" .. id
    SavedTexture.Value = Mesh.TextureId
end)
Mesh.TextureId = SavedTexture.Value -- Load previous texture
0
Didn't work. Wrongmistake 0 — 10y
0
Output? Ekkoh 635 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

ASSUMING YOU HAVE A LOCAL SCRIPT!

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

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

And then load them with this method:

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

function saveData(player)
    player:SaveInstance("SwordTextureId", swordMesh.TextureId)
end

function loadData(player)
    swordMesh.TextureId = player:LoadInstance("SwordTextureId")
end

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

game.Players.LocalPlayer.CharacterAdded:connect(function(character)
    loadData(game.Players.LocalPlayer)
end)

But this condition:

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

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

function onUpdate()
    if game.Players.LocalPlayer.Character.Humanoid.Health <= 0 then
        saveData(game.Players.LocalPlayer)
    end
    wait(0.5) -- Wait 0.5 seconds before calling again the function
    onUpdate() -- We call again the function
end

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

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

Answer this question