Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do You Make A Tool Stay Bought Even When A Player Rejoins?

Everything is fine and it says bought, but if you leave and rejoin, you still have the tool, but it says buy and you can buy it again, How do I make it to where it stays bought even when you rejoin? Heres the buy script-

local player = game.Players.LocalPlayer

local rs = game:GetService("ReplicatedStorage")

local tool = rs:WaitForChild("Chicken")

local stats = player:WaitForChild("leaderstats")

local button = script.Parent

cost = 200

  

script.Parent.MouseButton1Click:Connect(function()

script.Parent.Text = 'ItemOwned'

game.ReplicatedStorage.Events.Event1:FireServer(100)

if stats.Pounds.Value < cost then

script.Parent.Text = 'Need More Money'

wait(1)

script.Parent.Text = 'Buy Chicken - 200 Pounds'

end

end)

Here is the RemoteEvent Script-

local tools = game.ReplicatedStorage:WaitForChild("Chicken")

  

  

game.ReplicatedStorage.Events.Event1.OnServerEvent:Connect(function(player,tool)

local stats = player:FindFirstChild("leaderstats")

if stats.Pounds.Value >= 200 then

stats.Pounds.Value = stats.Pounds.Value - 200

game.StarterGui:WaitForChild("Mobile/Desktop Gui1"):WaitForChild("ShopGUI"):WaitForChild("Frame"):WaitForChild("Item2").Text = "ItemOwned"

game.StarterGui:WaitForChild("Mobile/Desktop Gui1"):WaitForChild("ShopGUI1"):WaitForChild("Frame"):WaitForChild("Item2").Text = "ItemOwned"

local clone = tools:Clone(tool)

clone.Parent = player.Backpack

local clone2 = tools:Clone(tool)

clone2.Parent = player.StarterGear

script:Destroy()

end

end)

Here is the tool datastore script-

local RunService = game:GetService("RunService")

local ToolData = game:GetService("DataStoreService"):GetDataStore("ToolData")

  

local ToolLocation = game.ServerStorage.Tools -- The location where all your tools are saved

  

function GetToolByName(toolName)

for _, tool in pairs(ToolLocation:GetChildren()) do

if tool.Name == toolName then return tool end

end

end

  

function IsRegisteredTool(tool)

for _, playerTool in pairs(ToolLocation:GetChildren()) do

if playerTool.Name == tool.Name then return true end

end

end

  

game.Players.PlayerAdded:Connect(function(player)

repeat wait() until player.Character

local dataKey = "tool-data_"..player.UserId

if ToolData:GetAsync(dataKey) then

for i, toolName in pairs(ToolData:GetAsync(dataKey)) do

local newStarterTool = GetToolByName(ToolData:GetAsync(dataKey)[i]):Clone()

local newTempTool = GetToolByName(ToolData:GetAsync(dataKey)[i]):Clone()

newStarterTool.Parent = player.StarterGear

newTempTool.Parent = player.Backpack

end

ToolData:SetAsync(dataKey, {})

end

end)

  

game.Players.PlayerRemoving:Connect(function(player)

local dataKey = "tool-data_"..player.UserId

local playerTools = {}

for _, tool in pairs(player.StarterGear:GetChildren()) do

if tool:IsA("Tool") and IsRegisteredTool(tool) then table.insert(playerTools, 1, tool.Name) end

end

if RunService:IsStudio() or #playerTools < 1 then return end

ToolData:SetAsync(dataKey, playerTools)

end)

4 comments

0

Just have a script that checks if the tool is inside the inventory and then if it is present, have the buy button inactive or whatever

0

That doen't really help

0

Doesn't

0

i've eddited the answer. check it out

1 answer

All what you need to do is check if the player already the item. To do that, just use condition:

local player = game.Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local tool = rs:WaitForChild("Chicken")
local stats = player:WaitForChild("leaderstats")
local button = script.Parent
cost = 200
-- this condiditon show "bought" if player already the item without pressing the button to check it.
if local item = player.Backpack:FindFirstChild(tool.Name) then
script.Parent.Text = "Bought"
end
    script.Parent.MouseButton1Click:Connect(function()
	    if player.Backpack:FindFirstChild(tool.Name) then -- if "Chicken" is in the Backpack then..
	    script.Parent.Text = "Bought"
	    else -- if player doesnt have chicken, allow him to buy one.
 	    game.ReplicatedStorage.Events.Event1:FireServer(100)
		    if stats.Pounds.Value < cost then
		    script.Parent.Text = 'Need More Money'
		    wait(1)
		    script.Parent.Text = 'Buy Chicken - 200 Pounds'
		    end
	    end
    end)

5 comments

0

It did not work

0

it shows a red line under

0

Red line under "local" on line 8

0

cause you havent defined item...

0

No i did.

This discussion is preserved as it was. New answers, comments, and votes are closed.