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!
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!
FindFirstChild
- Checks if a certain child exists. The function goes as so: Parent:FindFirstChild('CHILD_NAME')
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]
Destroy
- Pretty self-explanatory. Just destroys an object.
Touched
- Listens for when something touches the part the listener is set up to.
Players Service
- The ultimate service that handles all the players.
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. :)
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)