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

FE Shop only gives the item once then breaks?

this is a localscript that fires a remote that charges the player $300 for a baseball bat however it only works once then it doesn't work the second time and comes up with an error.Also my guy had enough money

script.Parent.MouseButton1Click:Connect(function()
	local plr=game.Players.LocalPlayer
	local cash=plr.Data.Wallet
	if cash.Value>=300 then 
		local event=game.ReplicatedStorage.backpack
		event:FireServer("Baseball Bat",300)
		script.Parent.Parent.Visible=false
	end
end)

The error is on the remote script's line 8



game.ReplicatedStorage.backpack.OnServerEvent:connect(function(plr,Message,cost)
	
	local pack=game.ServerStorage.Tools:FindFirstChild(Message)
	local cash=plr.Data.Wallet
	cash.Value=cash.Value-cost
	pack:Clone()
	pack.Parent=plr.Backpack
end)

error: https://gyazo.com/8da86a9c465e60591667ff85c24109a6

2 answers

game.ReplicatedStorage.backpack.OnServerEvent:connect(function(plr,Message,cost)

    if not Message then
        print("Invalid Item")
        return
    end

    local serverStorage = game:GetService("ServerStorage")
    local pack = serverStorage.Tools:WaitForChild(Message)
    local cash = plr.Data.Wallet

    cash.Value = cash.Value - cost
    pack:Clone()
    pack.Parent = plr.Backpack
end)

Try this maybe it will work for you.

2 comments

0

Now its saying "Infinite yield possible on 'ServerStorage.Tools:WaitForChild("Chicken Leg")'"

0

Well hopefully Chicken Leg is actually there. But you can add an extra parameter for a yeild counter and if it never finds the item within a certain amount of time just cancel the purchase. Ex: local pack = serverStorage.Tools:WaitForChild(Message, 10) if not pack then print("Couldn't locate item. Canceling purchase.") return end)

When you go to clone the item, dont make the variable and then clone. I believe you are actually just moving the item from replicated storage to the players Backpack. do this instead

game.ReplicatedStorage.backpack.OnServerEvent:connect(function(plr,Message,cost)

    local pack=game.ServerStorage.Tools:FindFirstChild(Message):Clone() -- add clone here
    local cash=plr.Data.Wallet
    cash.Value=cash.Value-cost
    pack.Parent=plr.Backpack
end)

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