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

When humanoid touches brick, brick deletes itself and adds to leaderstats?

Asked by 6 years ago

I am kind of new to scripting. I have tried to make a script that when the humanoid of the localPlayer touches a brick, it deletes itself and adds +1 to a certain leaderstat. This is the code:

RBXScriptSignal Touched:
    BasePart touchingPart,
    BasePart humanoidPart
local Part = Workspace.Part
Part:Destroy()
Part.Parent = Workspace
then
player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1

Please help me!

0
Well, props for trying... OldPalHappy 1477 — 6y
0
Oh lord..... T0XN 276 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You almost got it! :O It's pretty complicated, but how you'd accomplish this is using the Touched event of a part to listen for when a player touches the part, and then to execute code that adds to your money stat. Then, to get the player, we could use the GetPlayerFromCharacter function to check whether what touched the part was a player or not. (Neat thing about it is that it'll return the player too ^-^)

Now, ONTO TEH CODE!

local thePart = script.Parent -- In other words the script's parent. :P
local players = game:GetService('Players') -- The `GetPlayerFromCharacter` function requires the players server; in a nutshell, where all the players go when they join the game.

function addToStat(hit) -- Our function, named `addToStat`
    if hit.Parent then -- If what hit the brick wasn't a bullet. [They set their parent to nil after they hit something sometimes]
        local thePlayer = players:GetPlayerFromCharacter(hit.Parent) -- Get the player
        if thePlayer and thePlayer:FindFirstChild('leaderstats') and thePlayer.leaderstats:FindFirstChild('Money') then -- Check if what touched the part was a player, then check for `leaderstats` and `Money.`
            thePlayer.leaderstats.Money.Value = thePlayer.leaderstats.Money.Value + 1 -- GIVE DAT MAN SUM MOOLAH!
            con:Disconnect() -- Disconnects the listener. [No longer listens for a player to touch the part]
            thePart:Destroy() -- Destroy the part!
        end -- Ends if statement
    end -- Ends if statement
end -- Ends function

con = thePart.Touched:Connect(addToStat) -- Starts up the listener.

And that's pretty much all there's to it. <->

STUFF MENTIONED BUT NEVER REALLY TALKED ABOUT!

  1. FindFirstChild - Checks if a certain child exists. The function goes as so: Parent:FindFirstChild('CHILD_NAME')

  2. Events - In a nutshell, to connect the object to a listener to listen for something to happen. [Like the Touched event in the above code]

  3. Destroy - Pretty self-explanatory. Just destroys an object.

  4. Touched - Listens for when something touches the part the listener is set up to.

  5. Players Service - The ultimate service that handles all the players.

  6. GetPlayerFromCharacter - This function is pretty neat; in a nutshell, if what you gave it was a player's character, it'll return the player, and if not it'll return a boolean called nil. The function is as goes: Players:GetPlayerFromCharacter(Some_Model)

I hope this helped. :)

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

Try this

-- The script must be inside the brick you want to use this script on
function onTouched(hit) -- The function to find if a player stepped on the brick
    local player = game.Players[hit.Parent.Name] -- Finds the player who stepped on the brick
    player.leaderstats.Money.Value=player.leaderstats.Money.Value+1 -- This adds the money to the player
    script.Parent:Destroy() -- This destroys the brick
end

script.Parent.Touched:connect(onTouched)
0
Explain your answers. OldPalHappy 1477 — 6y
0
Im not good at explaining but do you see anything that needs to be explained so I can edit (: MasonTheCreeperYT3 74 — 6y
0
Well, because the asker doesn't know how functions and events work, it'd help to explain those. You don't have to, but your answer should at least link to someplace that does explain those. Aside from that, just walking the asker through your reasoning and what you did should be enough. OldPalHappy 1477 — 6y
0
Hope these edits are in someway better than before MasonTheCreeperYT3 74 — 6y

Answer this question