✓ Accepted answer
Problems Here
There are a couple of problems with your code here, those being that :
- mousebutton1click is an event, not a property
- local scripts cannot access the server storage, as it can only be accessed by server sided scripts
- you only cloned that objects and not parented them anywhere
- you did nothing with the cloned guis, or the Backpack
- FindFirstChild is a method, so you would have to use
: instead of .
- Cloning anything on the client is a very bad idea, for starters, it doesn't work with filtering enabled
Solution
With that said, here is a fixed version
Local Script
local RP = game:GetService("ReplicatedStorage")
local remote = RP.RemoteEvent
script.Parent.MouseButton1Click:Connect(function()
RP:FireServer()
script.Parent.Parent:Destroy()
end)
Server Script
local RP = game:GetService("ReplicatedStorage")
local remote = RP.RemoteEvent
local primary = RP.AK47
local secondary = RP.Baretta
RP.OnServerEvent:Connect(function(plr)
local p = primary:Clone()
local s = secondary:Clone()
p.Parent = plr.Backpack
end)
Hmm, that doesn't seem that safe
The scripts above do not really seem safe and rational to many people, and indeed, it isn't.
This is because, the client can too easily get multiples of the same tools simply by firing the remote multiple times.
To prevent this, you can simply use a boolean value to stop the client from simply firing the event multiple times to get multiple guns
Other takeaways here
A couple of other things you should take away here are that:
- Always make your scripts work with Filtering Enabled
- Don't let the client have too much power, use sanity checks when needed and handle important stuff on the server when possible
Hopefully that helped!
4 comments
Where would I put the ServerScript? As this is for a class GUI!
The server script, and server scripts in general should go in Server Script Storage
How would I make this function with more than once class?
Stop it, he is not writing your entire game for you