Giving cash on touch

im trying to make a part spawn and fall onto another part and when it touches that oart it gives 1 cash(leaderstats) to the player but i keep getting the same error


1 Like

Because player isn’t defined. Do this:

part.Touched:Connect(function(hit)
    local player = hit.Parent

still getting an error right here
Screenshot 2024-07-27 142406

You cannot get player from part.Touched, you have to use other methods such as:

  • Give an attribute to the money, that stores the player name
  • Define the player somewhere else
1 Like

GetPlayerFromCharacter

1 Like

Did you check the script? If I am not wrong, “hit” is the money not the player.

oh yeah you’re right, i was looking at the first Touched connection

1 Like

i still cant get it to give the player 1 cash

You need to check what touched the part and if it’s a humanoid you then get the parent of that humanoid, the character, and then GetPlayerFromCharacter as leaderstats are stored in the player and not the character. Additionally, i would recommend checking to see if what touched the part is a player but if it’s not check to see if it’s the part your looking for i.e. checking if it has the same name.

im smellin the grease from the event memory leak,.,.,
,.,.

on Touched event here I’ve never ever seen it being the parameters here, it just doesn’t exist and the code will see it as “a” and that’s not how you check the player, like what someone said already, you get the player from the character by checking if one of the character’s part had touched.

so how you can do it in the script is

part.Touched:Connect(function(hit)
  local player_src = player_service:GetPlayerFromCharacter(hit.Parent) -- get the 
end)

another one in the if statement is, you don’t need to check that for every seconds, just make a function and run it after changing the text property change

3 Likes

this is what should replace

local cash = Players[player.Parent.Name].leaderstats:FindFirstChild(‘cash’,true)

I can show example simple when part it’s touched by player or no

part.Touched:Connect(function(hit)
if hit.Parent and game.Players:FindFirstChild(hit.Parent.Name) then
print('Its player touching a part')
end
end)

you should detect player, touched function is a little bit messy but still, there are few things you should consider

local connection = part.Touched:Connect(function(hit: Basepart)
  local Humanoid = hit.Parent:FindFirstChild("Humanoid")
  if Humanoid then
    local player = game:GetService("Players"):GetPlayerFromCharacter(Humanoid.Parent)
    -- code to add money
  end

end

Edit: Also use task.wait() over wait() it’s better

To an addition to what was said above i have this script which i can share which uses said fixes and has pretty much the same desired function as yours plus some additional things including a debounce section which will stop multiple firings per touch:

-- Varibles
local BaseCashAdd = 5
local debounce = false
local MinRange = 0.1
local MaxRange = 5 
local DestroyPart = game.Workspace.DestroyPart


script.Parent.Touched:Connect(function(easports)

if debounce == true then  -- Debounce to avoid multiple cash additions per touch
	script.Parent.CanTouch = false
	task.wait(3)
	debounce = false
	script.Parent.CanTouch = true
	
	
elseif debounce == false then
	if game.Players:GetPlayerFromCharacter(easports.Parent) then -- Check if it's a player

		local player = game.Players:GetPlayerFromCharacter(easports.Parent)
		local lstats = player:FindFirstChild("leaderstats")
		local cash = lstats:FindFirstChild("Cash")
		
		local randomModifier = math.random(MinRange, MaxRange)
		local CashAdd = BaseCashAdd * randomModifier

		cash.Value = cash.Value + CashAdd
		print(CashAdd, randomModifier)
		debounce = true
		task.wait(1)
		
	elseif easports == DestroyPart then 
		script.Parent:Destroy()
	
	end
end
end)

I see that many people don’t understand that the hit param is not supposed to be a Player’s Character Descendant.

I guess this is a tycoon. What you can do is to make an ObjectValue with the Player as the value inside the Part with the touched event when the player claims the Tycoon.

1 Like