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

How can I make a part that gives a player a tool when stepped on?

I've tried this code, although it doesn't work.

script.Parent.Touched:Connect(function(hit) print("Hit") if hit.Parent.Humanoid then print("It was a player") if hit.Parent.leaderstats.Bucks.Value < 10 then print("You do not have enough bucks to purcahse a sword") elseif hit.Parent.leaderstats.Bucks.Value > 10 then print("You have enough bucks to purcahse a sword") local sword = game.ReplicatedStorage.ClassicSword:Clone() sword.Parent = hit.Parent.Backpack print("You have been given the sword") hit.Parent.leaderstats.Bucks.Value = hit.Parent.leaderstats.Bucks.Value -10 print("You have payed for the sword") end end end)

Does anybody know what's wrong with it?

6 comments

0

can you indent the code and use the little lua icon to make it easier to read please

0

the lua icon that shows up when you're editing your post*

0

yes pls format

0

also purchase is spelt wrong

0

how much bucks u have?

0

50

2 answers

✓ Accepted answer

It's simple. Here's a very short tutorial on how to do that.


  1. We'll store a tool inside of ReplicatedStorage, I stored a hammer called "hammer ".

  2. We'll now add the brick that gives the tool when touch.

  3. We'll now add a remote event to ReplicatedStorage, and name it "RemoteGiveTool".

  4. Now, we'll add a local script to the part, and type this :

function onTouch(hit)

    if hit.Parent.Humanoid ~= nil then

        game.ReplicatedStorage.RemoteGiveTool:FireServer()

end

script.Parent.Touched:Connect(onTouch)
  1. Now, we'll add a Script in ServerScriptService which is what the remote does when it's fired, and type this :
game.ReplicatedStorage.RemoteGiveTool.OnServerEvent:Connect(function(player)
      
     local cloneOfTool = game.ReplicatedStorage.hammer:Clone()

     cloneOfTool.Parent = player.Backpack
     
     
end    
  1. We're done! Now whenever you touch that brick it will clone the tool we placed in ReplicatedStorage and place it in the player's backpack. Hope it helped!

As far as your script goes, basically a lot of things went wrong. For example, I dont remember leaderstats existing in the character(hit.Parent), but rather the player of the character; So you should have searched for leadersats in the player of the character instead(you did put leaderstats in the player, right?). In addition, the way you checked for the money(player.leaderstats.Bucks.Value > 10) would be saying that you had to have over 10 dollars(e.g $10.1) to purchase the sword worth $10. The appropriate way to check for if the player has equal to or over $10 would be to use ">=" instead of ">". I made a rough edit of your code, which should work; though i cant assure you that:

local function getPlayerFromCharacter(character)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == character then
			return player
		end
	end
end

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Humanoid then
		print("it was a player")
		
		local player = getPlayerFromCharacter(hit.Parent)
		if player.leaderstats.Bucks.Value < 10 then
			print("u broke boi")
		elseif player.leaderstats.Bucks.Value >= 10 then
			print("purchasing sword...")
			local sword = game.ReplicatedStorage.ClassicSword:Clone()
			sword.Parent = player.BackPack
			print("sword acquired")
			player.leaderstats.Bucks.Value = hit.Parent.leaderstats.Bucks.Value - 10
			print("sword has been paid for")
		end
	end
end)

im sorry im rusty at this

2 comments

0

It says that "Backpack" isn't a valid member of Model in the output

0

sorry yeah i will edit that

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