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

Script Not Selling Item And Cloning It To The Players Backpack?

it worked before i added the script that would clone the tool, now it isn't working, any ideas?

local gui = script.Parent.Parent.Parent
local money = game.ReplicatedStorage.Money
local itemToClone = game.ServerStorage.Spaghetti
local player = game.Players.LocalPlayer
local button = script.Parent

script.Parent.MouseButton1Click:Connect(function()
	if money.Value >= 50 then
		local clonedItem = itemToClone:Clone()
		clonedItem.Parent = player.Backpack
		money.Value -= 50
		script.Click:Play()
	end
end)

2 comments

0

Any errors in the console?

0

You may want to give the player in question the tool on characteradded.

4 answers

This could be the issue:Play() is suppose to play a sound unless if you are wanting it to play a sound then that is not the issue.

Hope this helps!!

2 comments

0

the audio doesnt play but its supposed to be there

0

what's the audio name??

I assume you're using a local script that is a descendant of StarterGui. Place the "Spaghetti" tool from ServerStorage to ReplicatedStorage because the Client (where local scripts run) cannot access ServerStorage because judging from its name, it can only be accessed by the Server. The Server is the game itself, and only normal scripts can run in the Server-side.

The reason why you place it in ReplicatedStorage is because it can be accessed both the Server and the Client.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local gui = script.Parent.Parent.Parent
local money = ReplicatedStorage:WaitForChild("Money")
local itemToClone = ReplicatedStorage:WaitForChild("Spaghetti") -- i assume you already moved the tool in ReplicatedStorage
local player = Players.LocalPlayer
local button = script.Parent

button.MouseButton1Click:Connect(function()
	if money.Value >= 50 then
		local clonedItem = itemToClone:Clone()
		clonedItem.Parent = player.Backpack
		money.Value -= 50
		script.Click:Play()
	end
end)

But this still isn't the final asnwer..

I recommend using RemoteEvents because this script can be an advantage for hackers by changing the Money value in their client, so create a RemoteEvent inside ReplicatedStorage and name it CloneTool, and make a normal script inside ServerScriptService

Your local script in the button should look like this:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local gui = script.Parent.Parent.Parent
local removeEvent = ReplicatedStorage:WaitForChild("CloneTool") -- i assume you already created a RemoteEvent inside ReplicatedStorage and named it "CloneTool"
local player = Players.LocalPlayer
local button = script.Parent
local clickSound = script.Click

button.MouseButton1Click:Connect(function()
	task.spawn(clickSound.Play, clickSound) -- plays the sound
	remoteEvent:FireServer()
end)

And your normal script in ServerScriptService:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local removeEvent = ReplicatedStorage.CloneTool -- i assume you already created a RemoteEvent inside ReplicatedStorage and named it "CloneTool"
local itemToClone = ReplicatedStorage.Spaghetti -- i assume you already moved the tool in ReplicatedStorage
local money = ReplicatedStorage.Money

remoteEvent.OnServerEvent:Connect(function(player)
	if money.Value >= 50 then
		local clonedItem = itemToClone:Clone()
		clonedItem.Parent = player.Backpack
		money.Value -= 50
	end
end)

BUT THIS STILL ISN'T THE FINAL ANSWER!!

I recommend placing the Money value to each player instead because it can be changed by any player (and even the Server) if it's in ReplicatedStorage. So if you have any scripts that use ReplicatedStorage.Money YOU BETTER CHANGE THEM!

Final normal script in ServerScriptService:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local removeEvent = ReplicatedStorage.CloneTool -- i assume you already created a RemoteEvent inside ReplicatedStorage and named it "CloneTool"
local itemToClone = ReplicatedStorage.Spaghetti -- i assume you already moved the tool in ReplicatedStorage

Players.PlayerAdded:Connect(function(player)
	local money = Instance.new("IntValue", player)
	money.Name = "Money"
end)

remoteEvent.OnServerEvent:Connect(function(player)
	if player.Money.Value >= 50 then
		local clonedItem = itemToClone:Clone()
		clonedItem.Parent = player.Backpack
		player.Money.Value -= 50
	end
end)

sorry for long answer :P

5 comments

0

does the final answer matter if the game is fit for singleplayer only?

0

and what do i change replicatedstorage.money in the other scripts to?

0

answer 1: yes, it works regardless of the number of players

0

answer 2: if you're using replicatedstorage.money in a local script, use LocalPlayer.Money

0

i posted a answer to this question, can you review and see if i done it right

local Players = game:GetService("Players")
local gui = script.Parent.Parent.Parent
local money = game.ReplicatedStorage:WaitForChild("Money")
local itemToClone = game.ServerStorage:WaitForChild("Spaghetti")
local button = script.Parent

button.MouseButton1Click:Connect(function(plr)
    if money.Value >= 50 then
        local clonedItem = itemToClone:Clone()
        clonedItem.Parent = plr.Backpack
        money.Value -= 50
        script.Click:Play()
    end
end)

1 comment

0

The MouseButton1Click event has no parameters; this will only error, as "plr" is nil.

for T3_MasterGamer

local Players = game:GetService("Players").LocalPlayer
local money = Players.Money
local multiplier = game.ReplicatedStorage.Multiplier
debounce = false

script.Parent.Touched:connect(function(hit)
	if not debounce then
		debounce = true
		if(hit.Parent:FindFirstChild("Humanoid")~=nil)then
	script.Parent:Destroy()
			money.Value += 1 *  multiplier.Value
		end
		debounce = false
	end
end)

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