I tried to create this Buy script but it’s not working what could be the problem?
Script:
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.Coins.Value >= 20 then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 20
game.ReplicatedStorage.Tools.Item1:Clone().Parent = player:WaitForChild("Backpack")
end
end)
Tools folder is in the replicated storage as you can see:
Item 1 is inside the Tools folder
local player = game.Players.LocalPlayer
local storage = game:GetService("ReplicatedStorage")
script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.Coins.Value >= 20 then
player.leaderstats.Coins.Value -= 20
local toolClone = storage:WaitForChild("Tools"):WaitForChild("Item1"):Clone()
toolClone.Parent = player:WaitForChild("Backpack")
end
end)
If you want the stats to replicate to the server then you’ll either need to make this a server script or have a “RemoteEvent” instance fire the server.
I suggest doing the tool parenting and money checking in a server script to prevent exploiters. A localscript would look like this;
script.Parent.MouseButton1Click:Connect(function()
REMOTE_EVENT_PATH:FireServer(tool_name)
end
A server script
REMOTE_EVENT_PATH.OnServerEvent:Connect(function(player, toolName)
if player.leaderstats.Coins.Value >= 20 then
player.leaderstats.Coins.Value -= 20
local toolClone = storage:WaitForChild("Tools"):WaitForChild(itemName):Clone()
toolClone.Parent = player:WaitForChild("Backpack")
end
end)
The remote event path would look like this game.ReplicatedStorage.REMOTEVENTNAME
You can name the remote event anything you want.
--LOCAL SCRIPT--
local player = game.Players.LocalPlayer
local storage = game:GetService("ReplicatedStorage")
local re = storage:WaitForChild("BuyRemote")
script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.Coins.Value >= 20 then
re:FireServer()
end
end)
--SERVER SCRIPT--
local storage = game:GetService("ReplicatedStorage")
local re = storage:WaitForChild("BuyRemote")
re.OnServerEvent:Connect(function(player)
if player.leaderstats.Coins.Value >= 20 then
player.leaderstats.Coins.Value -= 20
local toolClone = storage:WaitForChild("Tools"):WaitForChild("Item1"):Clone()
toolClone.Parent = player:WaitForChild("Backpack")
end
end)