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

can anyone help my tool is disappearing? Ive tried server sided and client sided and both?

so basically im trying to make it so when you buy this certain thing it lowers the value of "trees" and give the player their certain thing but it works and all but when it gives the player their thing in their inventory is just dissappears see the value being subtracted and the cloning and the destroying of the axe all works but the item is not existent ive tried moving "wta" the item to workspace to replicated storage to server storage and nothing

local BuyButton = script.Parent
local plr = game.Players.LocalPlayer
local axefolder = plr:WaitForChild("leaderstats")
local trees = axefolder.Trees
local axe = plr.Backpack.Axe
local plrh = plr.Character
local wta = workspace.Axes["White Trail Axe"]

BuyButton.MouseButton1Down:Connect(function()
    if trees.Value >= 10 then
        trees.Value = trees.Value - 10
            axe:Destroy()
            local wtahClone = wta:Clone()
            wtahClone.Parent = plr.Backpack
    else
        print("man you broke as hell")
    end
    
end)

1 answer

✓ Accepted answer

Trying this in server will error because the events in the BuyButton will only be fired in the client, and game.Players.LocalPlayer will return nil since it only exists in the client. Trying this in client won't get an error but you can only see the wta and the axe being removed to yourself and not to everyone. From the title, it said that you used both, but are you sure you really used both? Maybe you didn't used RemoteEvents.

All of the script will be in the client except for the part where you destroy the normal axe and put wta to the player's inventory. First, you create a RemoteEvent inside ReplicatedStorage. Then, name it WTAEvent (you can name it whatever you want but you must name it this for the sake of my answer). And lastly, you create a script inside ServerScriptStorage.

Here's the script for the client (button script):

local BuyButton = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("WTAEvent")

BuyButton.MouseButton1Down:Connect(function()
	WTAEvent:FireServer()
end)

And for the server (in ServerScriptService):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage.WTAEvent

event.OnServerEvent:Connect(function(player)
	local axefolder = player.leaderstats
	local trees = axefolder.Trees
	
	local axe = player.Backpack:FindFirstChild("Axe")
	local wta = workspace.Axes:FindFirstChild("White Trail Axe")
	
	if trees.Value >= 10 then
		trees.Value -= 10
		
		if axe then -- if the axe exists in the player's inventory
			axe:Destroy()
			local wtaClone = wta:Clone()
			wtaClone.Parent = player.Backpac
		end
	else
		print("man you broke as hell")
	end
end

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