You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
A proper shop that could give the player a tool
What is the issue?
I made the shop, and it does give the player the correct tool,
But, when the player equips the item and activates it, The script of the tool does not work
I am using the Bloxy Cola gear which does contain a proper tool Script
What solutions have you tried so far?
I tried to use ServerStorage instead of ReplicatedStorage, does not work
I tried to use a Script instead of the LocalScript
The code
local stats=game.Players.LocalPlayer:WaitForChild("Statistics")
script.Parent.Activated:Connect(function()
if stats.Coins.Value >= script.Parent.Parent.Price.Value then
stats.Coins.Value=stats.Coins.Value-script.Parent.Parent.Price.Value
if stats.Coins.Value < 0 then
game.Players.LocalPlayer:Kick("Error: Bad Value [400]")
end
local Cola=game.ReplicatedStorage.ShopItems.BloxyCola:Clone()
Cola.Parent=game.Players.LocalPlayer:WaitForChild("Backpack")
end
end)
You cannot clone a Tool on the client and expect it to replicate to the server. You must clone the tool on the server and then parent it into the players backpack.
local stats=game.Players.LocalPlayer:WaitForChild("Statistics")
local RemoteEvent=game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")
script.Parent.Activated:Connect(function()
RemoteEvent:FireServer(stats, script.Parent.Parent.Price.Value) -- send the stats and price to the server, don't need to send the player since this is a local script.
end)
Server script:
local RemoteEvent=game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")
RemoteEvent.OnServerEvent:Connect(function(player, stats, price) --gets the player, stats, and price sent over
if stats.Coins.Value >= price then
stats.Coins.Value=stats.Coins.Value-price
if stats.Coins.Value < 0 then
player:Kick("Error: Bad Value [400]")
end
local Cola=game.ReplicatedStorage.ShopItems.BloxyCola:Clone()
Cola.Parent=player:Kick:WaitForChild("Backpack")
end
end)
I tried to stick to your coding style, just to help you out!
You can also do this without remote events. Replace the “Purchase” LocalScript with this Script and move the BloxyCola to ServerStorage.
Code:
local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local stats = player:WaitForChild("Statistics")
script.Parent.MouseButton1Click:Connect(function()
if stats.Coins.Value >= script.Parent.Parent.Price.Value then
stats.Coins.Value = stats.Coins.Value - script.Parent.Parent.Price.Value
if stats.Coins.Value < 0 then
player:Kick("Error: Bad Value [400]")
end
local Cola = game.ServerStorage.ShopItems.BloxyCola:Clone()
Cola.Parent = player:WaitForChild("Backpack")
end
end)
This also makes your code invulnerable to exploiters, as remote events could leave your game vulnerable to exploiters depending on how you script it. For example, a player could fire the remote with a very low price, or they could fire the remote with a very high price and pass in someone else’s stats to basically take away most of their money.
Nathanator58, uh, you cannot listen for the GuiButton.MouseButton1Click event on the server. You can only listen for it in a LocalScript. Hence, suggesting Potatoegy uses a RemoteEvent approach.