Tool's script does not work after being cloned

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    A proper shop that could give the player a tool

  2. 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

  3. 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)

The explorer
image
image

1 Like

since you’ve tried to use the script as a server script. You can’t use game.Players.LocalPlayer

Try using RemoteEvents/RemoteFunctions to get the player whenever you call “script.Parent.Activated:Connect(function()” in a local script.

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.

I know that you cannot use LocalPlayer in a Script
I haven’t experience that issue yet

1 Like

Is this a free model? The script seems to be very unsecure and for obvious reasons, will not work.

no i wrote it myself

30characters

1 Like

So i have to use Script instead of LocalScript?
i tried to use it, but the Tool simply did not clone into the Player’s backpack

1 Like

Ok well, I’d advise you to read up on what RemoteEvents and RemoteFunctions are before attempting something like this, here’s a link.

example:

local script:

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! :smile:

1 Like

i will test it out, thanks for helping :wink:

2 Likes

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.

I will try that out, thanks for helping tho

2 Likes

oops, I made a typo, check out the server script code again.

okay i see

30characterstahnksdakmsdklm

2 Likes

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.

1 Like

Really? I always use server scripts to handle the MouseButton1Click event and I haven’t had any issues with it so far.

You sure about that…? Is your game FilteringEnabled?

Yeah, my game is FilteringEnabled.

Well everyone I’ve spoken to says that you cannot do anything UI-Related on the server since it’s client-based.

if you do gui stuff with a server script it’ll appear for everyone.
most of the time at least, i think. I don’t use server scripts for guis.