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

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

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

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)

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