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

How to allow player to buy a gun?

Not gonna lie, my language is C#, not Lua, so I've just yolo'd this project and I need help with something. I'm making a game (obv), and I need to make it where the player can buy this AR when they click it. Here's my code that isn't working:

Player = game.Players.LocalPlayer

script.Parent.ClickDetector.MouseClick:Connect(function()
			
	if Player.LeaderBoard.Money.Value >= 500 then
		Player.LeaderBoard.Money.Value = Player.LeaderBoard.Money.Value - 500
	
    	game.ReplicatedStorage.M18:clone().Parent = Player:WaitForChild("Backpack")
	else
			
	end		
end)

Please help.

1 answer

✓ Accepted answer

Hello.


Problem:

  • You used LocalPlayer in a ServerScript. This will not work as LocalPlayer only works on LocalScripts.

Solution:

  • Use the player argument of ClickDetector.MouseClick instead of LocalPlayer.

Recommendations:

  • Use Clone() with an uppercase "C" as clone() is deprecated. It is never a good idea to use deprecated functions.

  • You do not have to use WaitForChild() on the player's backpack as it will be loaded in.

  • Use GetService() when indexing services as it checks if the service is existent. If it isn't it creates the service. GetService() is also useful if someone renamed the services. GetService() is the only way to create services.

  • You do not need the else for the if statement on line 02.


Fixed Script:

script.Parent.ClickDetector.MouseClick:Connect(function(Player)
	if Player.LeaderBoard.Money.Value >= 500 then
		Player.LeaderBoard.Money.Value = Player.LeaderBoard.Money.Value - 500
		game:GetService("ReplicatedStorage").M18:Clone().Parent = Player.Backpack 
	end     
end)

Also, put local before declaring a variable unless you're using a global variable. Local variables are faster and more efficient than global ones though.

5 comments

0

thank you! been trying to do this FOREVER

0

No problem. Please accept my answer.

0

how do i do that? lmao

0

For new users, there's been a bug going around that you have to go into incognito mode to accept an answer. The button should be under my answer.

0

ahh, yeah, had to go incog.

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