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

How do I keep a purchased item inside a player's inventory after a re-spawn?

Asked by 7 years ago

Hello!

I have a purchase script below that allows a player to successfully add an item to their inventory after purchase from the in-game shop and then prevents them from re-purchasing a duplicate item. But I have two problems.

First - after the player re-spawns, the item disappears and is no longer in their inventory. If the player then tries to repurchase the item they're unable to, as they receive the notice that they've already purchased it - which is intended to prevent accidental duplicate purchases. But, how do I make the item survive the respawn and remain usable to the player?

Here is the purchase script below:

local plr = game.Players.LocalPlayer
local replicatedstorage = game:GetService('ReplicatedStorage')
local amount = 100
local points = plr.leaderstats.Points
script.Parent.MouseButton1Click:connect(function()
    if plr.Backpack:FindFirstChild('RocketLauncher') or plr.StarterGear:FindFirstChild('RocketLauncher') or game.Workspace[plr.Name]:FindFirstChild('RocketLauncher')then 
         script.Parent.Parent.TextLabel.Text = 'You have already bought this item!'
            wait(3)
         script.Parent.Parent.TextLabel.Text = 'Welcome to the shop'
         else

    if plr.leaderstats.Points.Value >= amount then 
            replicatedstorage.RocketLauncher:Clone().Parent = plr.Backpack 
            replicatedstorage.RocketLauncher:Clone().Parent = plr.StarterGear 
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - amount          
else 

local pointsrequired = amount - points.Value 
    script.Parent.Parent.TextLabel.Text = 'You need '..pointsrequired..' more Points to buy this item!'
        wait(3)
    script.Parent.Parent.TextLabel.Text = 'Welcome to the shop'
end
end
end)

Second - After the item is purchased, how do I disable the use of items while inside the Lobby? All of my scripting attempts result in failure, or stops the game. I tried adapting an old Healing Block script by inserting a block to take up the entire lobby with a disabling backpack script, but I've made a real mess of the code:

local isOn = true
function onTouch(hit)
if (isOn == false) then return end
    local human = hit.Parent:findFirstChild("Humanoid")
    if (human ~= nil) then
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local dis = script:FindFirstChild("Disable"):Clone()
    if dis then
        local plrGui = plr:WaitForChild("PlayerGui")
        dis.Parent = plrGui
        dis.Disabled = false
end
end)
turnOff()
end
end
connection = script.Parent.BackPackDisabler.Touched:connect(onTouch)
while true do
    wait(cooldown)
    if (isOn == false) then turnOn() end
end

Thank you very much for any assistance! :)

0
put it in StarterGear nanaluk01 247 — 7y
0
I did, but when the character re-spawns, the item is no longer accessible. Never2Humble 90 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
local wm = Instance.new("Model")
wm.Name = "Weaps"

function give(weap, player) --add a weap to a player's weaps
    --return if they allready have a copy
    for _, w in ipairs(player.Weaps:GetChildren()) do
        if w.Name == weap.Name then return end
    end
    --return if the weaps is a startpack one
    for _, w in ipairs(game.StarterPack:GetChildren()) do
        if w.Name == weap.Name then return end
    end
    --add it to thier weaps
    weap:clone().Parent = player.Weaps
end

game.Players.ChildAdded:connect(function(p)
    wm:clone().Parent = p
    wait()
    p.Character.ChildAdded:connect(function(wep)
        if wep.className == "Tool" then
            give(wep, p)
        end
    end)
    p.Changed:connect(function(pr)
        if pr == "Character" then
            for _, w in ipairs(p.Weaps:GetChildren()) do
                w:clone().Parent = p.Backpack
            end
            p.Character.ChildAdded:connect(function(wep)
                if wep.className == "Tool" then
                    give(wep, p)
                end
            end)
        end
    end)
    p.Backpack.ChildAdded:connect(function(h)
        if h.className == "HopperBin" then
            give(h, p)      
        end
    end)
end)


0
GotSanity - Thanks for the code, but I'm a bit confused, do I just place the code underneath my purchase script? Or in a separate local script with the purchased item? Never2Humble 90 — 7y
0
Just place the script in workspace. GotSanity 16 — 7y
0
Thank you! :) Never2Humble 90 — 7y
0
GotSanity - This code does not work. I still can't regain the items after the player re-spawns. Never2Humble 90 — 7y
0
This was solved in the strangest way. I opened up a new session of studio, copied all of the contents from the old one into the new one...and it worked. I changed nothing. I don't understand what happened, but as long as it works. Roblox works in mysterious ways I guess. Never2Humble 90 — 7y
Ad

Answer this question