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

My Class GUI Won't function with no error statements?

Hello, I've recently made a GUI with classes in it, for when the player joins the game, they'll have to select one of said classes to use in game, and after selection, the frame is destroyed. Why won't this function without error codes?

local primary = game.ServerStorage.AK47
local secondary = game.ServerStorage.Baretta
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function(click)
if script.Parent.MouseButton1Click == true then
   player.FindFirstChild("Backpack")
     primary:Clone()
     secondary:Clone()
    script.Parent.Parent:Destroy()
    end
end)

1 answer

✓ 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

0

Where would I put the ServerScript? As this is for a class GUI!

0

The server script, and server scripts in general should go in Server Script Storage

0

How would I make this function with more than once class?

0

Stop it, he is not writing your entire game for you

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