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

How to get the player stats in a touched part ? (Tycoon)

Asked by 5 years ago
Edited 5 years ago

Because i wanna make a tycoon conveyor

local plr = game.Players:GetPlayers()

script.Parent.Touched:Connect(function(hit)
        print(hit.Parent.Box)
        hit.Parent.Box:Destroy()
        plr.Stats.Cash.Value = 1
        print(plr.Stats.Cash.Value)
end)

1 answer

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Assuming you are doing this all in a server Script:

Using :GetPlayers() returns a table of all players inside of the server. You usually use this when you want to get the number of players or do something with all of the players. A better method can be used in conjunction with the PartThatHit parameter in the Touched event.

I'm talking about using :GetPlayerFromCharacter():

--Get our Players service.
local Players = game:GetService("Players")

local part = script.Parent

part.Touched:Connect(function(partThatHit)
    if partThatHit.Parent:FindFirstChild("Humanoid") then
        --If the parent of the part that's touching has a humanoid
        --then we know it's a character!
        local character = hit.Parent
        local player = Players:GetPlayerFromCharacter(character)

        character.Box:Destroy() --Could check for this

        local cash = player.Stats.Cash
        cash.Value =  cash.Value + 1 --Increment cash by 1

        print(cash.Value) --prints value of cash AFTER we increment
    end
end)

Ad

Answer this question