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

Tool isn't being added into backpack and starter gear?

Hello, I've made a local script so when a player presses a button the tool is cloned into the players' backpack and starter gear through replicated storage. However, after trying to buy the item it doesn't seem to work, what is the problem as there are no errors coming up on the developer console.

local player = game.Players.LocalPlayer
local Repstorage = game:GetService("ReplicatedStorage")
local leaderstats = player:WaitForChild("leaderstats")
local price = script.Parent:WaitForChild("Price")
local ForgetMeNotTool = game.ReplicatedStorage:WaitForChild("Flashlight")


script.Parent.MouseButton1Click:Connect(function()
 leaderstats.Money.Value = leaderstats.Money.Value - 3000
ForgetMeNotTool:Clone().Parent = player.Startergear
ForgetMeNotTool:Clone().Parent = player.Backpack
end)

1 answer

✓ Accepted answer

For first, you need to use RemoteEvent to send the informations to server and server give items and others.

Create a RemoteEvent and put in ReplicatedStorage. Now create a Script and put on ServerScriptService and put this code:

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") -- Your event location.
local ForgetMeNotTool = game.ReplicatedStorage:WaitForChild("Flashlight") -- Your tool

event.OnServerEvent:Connect(function(player,item) -- On fire get player
	local leaderstats = player:WaitForChild("leaderstats") -- Wait for leaderstats
	local price = game.ReplicatedStorage.Prices[item]:WaitForChild("Price") -- Get price / Put price location. for this create a folder named as Prices in replicated storage. put item name and on Click send the item
	if leaderstats.Money.Value >= price.Value then -- If have money for buy
		leaderstats.Money.Value = leaderstats.Money.Value - price.Value -- remove money
		--Give tool to player.
		ForgetMeNotTool:Clone().Parent = player.Backpack
		ForgetMeNotTool:Clone().Parent = player.StarterGear
	end
end)

Now. create a LocalScript in your button and put this script:

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") -- Your event location.

local item_name = script.Parent.Name -- Put item name

script.Parent.MouseButton1Click:Connect(functoin()
	event:FireServer(item_name) -- On click fire the event and send the item for server.
end)

Remote Events are designed to provide a one way message between the server and clients. This message can be directed from one client to the server, from the server to a particular client, or from the server to all clients.

Wiki pages:

RemoteEvent/Function

Hope it helped :)

Errors? tell-me on comments.

2 comments

0

My goal was to not use remote events since it uses up memory, is there another way to do this without using remote events?

0

No

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