What's wrong with this script?

Hiya Developers!

I have a system here, where if the slected number is one, we give the player a tool from replicated sorage to there “Backpack”, but it doesnt seem to work.

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		print("Hit")
		print(ToolNumber1)
		if ToolNumber1 == 1 then 
			print("Tool 1 Was Given To The Player ")
			if ToolNumber1 == 1 then 
				local Sword = game.ReplicatedStorage.Tools:WaitForChild("ClassicSword")
				function onTouched(hit)
					if hit.Parent:findFirstChild("Humanoid") then
						
					end

					local Clone = Sword:Clone()
					Clone.Parent = player.Backpack
				end)

Any help, im new to scripting!

1 Like

Can you show the whole script, please.

local ToolNumber1 = math.floor(1, 1) --Returns a number between 1 and 10.
local ToolNumber2 = math.floor(1, 1) --Also returns a number between 1 and 10.
print(ToolNumber1, ToolNumber2) 


-- Randomly Pick The Tool

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		print("Hit")
		print(ToolNumber1)
		if ToolNumber1 == 1 then 
			print("Tool 1 Was Given To The Player ")
			if ToolNumber1 == 1 then 
				local Sword = game.ReplicatedStorage.Tools:WaitForChild("ClassicSword")
				function onTouched(hit)
					if hit.Parent:findFirstChild("Humanoid") then
						
					end

					local Clone = Sword:Clone()
					Clone.Parent = player.Backpack
				end)
			end
		end

I know math.floor is currently a 100% chance of “1”, I’m testing that’s why.

local ToolNumber1 = math.random(1, 10) --Returns a number between 1 and 10.
local ToolNumber2 = math.random(1, 1) --Also returns a number between 1 and 10.
print(ToolNumber1, ToolNumber2) 


-- Randomly Pick The Tool

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		print("Hit")
		print(ToolNumber1)
		if ToolNumber1 == 1 then 
			print("Tool 1 Was Given To The Player ")
			if ToolNumber1 == 1 then 
				local Sword = game.ReplicatedStorage.Tools:WaitForChild("ClassicSword")
				function onTouched(hit)
					if hit.Parent:findFirstChild("Humanoid") then
						
					end

					local Clone = Sword:Clone()
					Clone.Parent = player.Backpack
				end)
			end
		end
	end
end)

???

Thanks, ill try and see if this works now.

Doesn’t seem to work, no errors are printed. This is the only issue shown in the script.

image

local ToolNumber1 = math.random(1, 10) --Returns a number between 1 and 10.
local ToolNumber2 = math.random(1, 1) --Also returns a number between 1 and 10.
print(ToolNumber1, ToolNumber2) 


-- Randomly Pick The Tool

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		print("Hit")
		print(ToolNumber1)
		if ToolNumber1 == 1 then 
			print("Tool 1 Was Given To The Player ")
			if ToolNumber1 == 1 then 
				local Sword = game.ReplicatedStorage.Tools:WaitForChild("ClassicSword")
				function onTouched(hit)
					if hit.Parent:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
						local player = game.Players:GetPlayerFromCharacter(hit.Parent)
						
						local Clone = Sword:Clone()
						Clone.Parent = player.Backpack
					end
				end)
			end
		end
	end
end)

Try now.

1 Like

Nope, it prints everything, but doesn’t give the tool?

image

1 Like

This works: i simply had to remove

1 Like

Okay but then, do you mind marking my message as the solution if that will help.

1 Like