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

(ANSWERED/FIXED) Why cant I send a tool to the players Backpack?

SOLVEDI have a leaderstats script, number value, and everything so the script works because I tested it with the print command, I just can't send a tool to the player's backpack and I have no idea why can anyone help me? edit: sorry for the bold

Script:

--variables
local price = script.Parent.PRICE
local name = script.Parent.Name
local tool = game.ReplicatedStorage:FindFirstChild(name)
local player = script.Parent.Parent.Parent.Parent.Parent.Parent


script.Parent.MouseButton1Click:connect(function()
	if player.leaderstats:FindFirstChild("Cash").Value >= price.Value then --I have a leaderstats script and everything works except the end line
		print("cash good yes")
		player.leaderstats:FindFirstChild("Cash").Value = player.leaderstats:FindFirstChild("Cash").Value - price.Value
		local clone = tool:Clone()
		clone.Parent = player.Backpack --error
		end
	end)

6 comments

0

I'm still confused is "game.Players.PlayerAdded:Connect(function(Player)" the same "player" as my variable?

0

The problem could be in the local player variable. You may have added too many "Parent"s.

0

No.

0

Do I remove the entire variable and do something else?

0

I fixed it^^^

1 answer

  1. I think you're trying to get a player by using game.Players. To reference a player in a LocalScript, you have to use game.Players.LocalPlayer. If you're trying to get a player from a server script, there are different ways for how to do so.

  2. I'm assuming you're using a LocalScript. If you aren't, then well there's still something wrong. You'd have to use a RemoteEvent because if all this is in just a LocalScript, the client can't access what only a server script can do, which is what you are trying to do here. You want to change the amount of cash a player has when a player buys the tool.

Insert a RemoteEvent in ReplicatedStorage.

I'll try to help you out by showing you what you may want in your scripts:

LocalScript (keep it in the same place that your script was in and you can delete the one you have right now)

script.Parent.MouseButton1Click:Connect(function(player)
	game.ReplicatedStorage.RemoteEvent:FireServer(player)
end)

Script (in ServerScriptService)

local price = script.Parent.PRICE -- You might want to change the script.Parent since this script is in a different location.
local name = script.Parent.Name -- You might want to change the script.Parent since this script is in a different location.
local tool = game.ReplicatedStorage:FindFirstChild(name)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
    if player.leaderstats:FindFirstChild("Cash").Value >= price.Value then --I have a leaderstats script and everything works except the end line
        print("cash good yes")
        player.leaderstats:FindFirstChild("Cash").Value = player.leaderstats:FindFirstChild("Cash").Value - price.Value
        local clone = tool:Clone()
        clone.Parent = player.Backpack --error
        end
    end)

Hope this helps!

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