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
Because player isnât defined. Do this:
part.Touched:Connect(function(hit)
local player = hit.Parent
still getting an error right here
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
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
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
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.