Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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?

0
can you indent the code and use the little lua icon to make it easier to read please ThisIsAnAccount255 143 — 4y
0
the lua icon that shows up when you're editing your post* ThisIsAnAccount255 143 — 4y
0
yes pls format TheluaBanana 946 — 4y
0
also purchase is spelt wrong TheluaBanana 946 — 4y
View all comments (2 more)
0
how much bucks u have? TheluaBanana 946 — 4y
0
50 soccerstardance251 29 — 4y

2 answers

Log in to vote
0
Answered by
XDvvvDX 186
4 years ago

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)

5) 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    

6) 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!

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
It says that "Backpack" isn't a valid member of Model in the output soccerstardance251 29 — 4y
0
sorry yeah i will edit that TheluaBanana 946 — 4y

Answer this question