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

Make a script that makes sure their are no other copies of tools?

Hello I'm scripting a dealer that gives weapons and I'm trying to script a system that doesn't allow the client to take multiple identical tools at the same time

local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
	if not player.Backpack:FindFirstChild("Coltello") or player.Backpack:FindFirstChild("Beretta 92FS") or player.Backpack:FindFirstChild("Glock 17") then
-- stuff that allows to buy the gun etc
end
end)

the problem is that when a player tries to buy multiple different guns it won't allow it due to the if statement, can anyone help me?

1 comment

0

You need to make some way to specify what gun exactly are you buying and then only having one if statement, otherwise you will always keep having this problem.

1 answer

Just create a function that gives the tool, and checks to make sure they dont have it all in one.

--For this example I will assume you have a folder inside ReplicatedStorage named Tools, and all available tools are stored here.
-- You have to define player yourself, this script needs to be in a server script, so remote events are probably required, unless you are using touched events on parts to handle the purchase.
local function GiveTool(player,Tool) -- Define Tool as the name of the tool. (Ex:   GiveTool(player,"Glock 17")
	if player ~= nil and Tool ~= nil then -- Make sure function has all arguements
		local Backpack = player.Backpack -- Define players backpack
		local Character = player.Character -- Define players character, because a tool that is equipped is stored here instead of backpack
		local hastool = Backpack:FindFirstChild(Tool) -- See if tool is in backpack
		if hastool == nil then -- If not then
			hastool = Character:FindFirstChild(Tool) -- See if tool is on player's character
		end
		if hastool == nil then -- If no tool was found
			local newtool = game.ReplicatedStorage.Tools[Tool]:Clone() -- Give new tool.
			newtool.Parent = Backpack
		end
	end
end
GiveTool(Player,"Glock 17")

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