Help with Function()

Hello there! I need some help with my script!

function buy()
	local player = game.Players.LocalPlayer
	local Tool = player.Backpack:FindFirstChild("Radar")
	local C4 = game.Lighting.OKand
	local cloned = C4:Clone()
	local Tool2 =  player.Backpack:FindFirstChild("Gift")
	if Tool and Tool2 then
		cloned.Parent = player.Backpack
		Tool:Destroy()
		Tool2:Destroy()
	end
end
script.Parent.MouseButton1Down:connect(buy)

snippet

that’s where the script is.

so basically this script is supposed to detect if a player has the tools both (“Radar”, and “Gift”)
when it detects both of them its supposed to remove them and give a new tool call “C4”
but i dont know what im doing wrong

1 Like

please put in lua next time so we can read it.

local button = script.Parent
function buy()
local player = game.Players.LocalPlayer
local Tool = player.Backpack:FindFirstChild('Radar')
local C4 = game.Lighting.OKand
local cloned = C4:Clone()
local Tool2 = player.Backpack:FindFirstChild('Gift')
if Tool and Tool2 then
cloned.Parent = player.Backpack
Tool:Destroy()
Tool2:Destroy()
end
end
script.Parent.MouseButton1Down:Connect(buy)

Use remote events to destroy it from the server.

This is extremely difficult to read, I can’t tell what’s happening please use

lua

and have it properly indented

1 Like

how to “lua”? … sry im not that experienced and i would appreciate it

you use the accent symbol on your keyboard 3 times ``` at the beginning of the code and at the end of the code

The tool will only be removed from the client side, plus this is done on a SERVER SCRIPT which you will not be able to get the LocalPlayer that way

You have to use RemoteEvents if you want to destroy the tools from the server side, and change it to a LocalScript:

--Client
local Button = script.Parent
local Player = game.Players.LocalPlayer
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

local function Buy()
    local Tool = player.Backpack:FindFirstChild("Radar")
    local Tool2 = player.Backpack:FindFirstChild("Gift")

    if Tool and Tool2 then
        Event:FireServer() --Instead of destroying the tools on the client side, instead fire an event to the server to destroy the tools that way
    end
end

script.Parent.MouseButton1Down:Connect(Buy)
--Server Script inside ServerScriptService
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local C4 = game.Lighting.OKand

Event.OnServerEvent:Connect(function(Player)
    Player.Backpack.Radar:Destroy()
    Player.Backpack.Gift:Destroy()
    
    local GearClone = C4:Clone()
    GearClone.Parent = Player.Backpack
    
    local GearClone = C4:Clone()
    GearClone.Parent = Player.StarterGear
end)
3 Likes

Thanks for the help, sorry for not doing that lua thing in the beginning lol :sweat:!!!

It’s fine :sweat_smile: Just make sure to encase your code in code blocks like these:

Hello I am a code block by using this: ```Code block```
1 Like