You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I have a collector script for a tycoon that collects drops from a dropper and increases a leader stat value.
What is the issue? Include screenshots / videos if possible!
Whenever a drop goes into the collector, it gives the error: Workspace.Tycoons.Tycoon.Items.Conveyor.Colllector.Script:11: attempt to index nil with ‘leaderstats’
I’ve tried, I don’t know what to do. The script is a regular script inside of a replicated storage that is then later cloned into workspace.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried changing the “player” value, and more, but nothing worked.
local cPart = script.Parent
cPart.Touched:Connect(function(hit, player)
print("something detected")
print(player)
if hit:GetAttribute("IsDrop") == true then
local dropAmount = hit:GetAttribute("Amount")
hit:Destroy()
print("cool! drop detected")
player.leaderstats.Cash.Value += dropAmount
end
end)
This is my code, the cPart is the collector part itself.
EDIT: I won’t be back for ~7 hours after posting this, please be patient.
Does this indeed print the player name? If it does, then make sure you are actually getting the player, and not the player’s character. You can do that by using the GetPlayerFromCharacter(character) function.
If you are not getting a name, then acquiring two variables in the function is causing an error, and you have to find a different way to get the player. If the hit is a child or descendant of the player, then use hit.Parent or something of similar manner to connect to the model of- or the player itself. Then use the function named before; GetPlayerFromCharacter(hit.Parent) to get the player.
local cPart = script.Parent
cPart.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
print("something detected")
print(player)
if hit:GetAttribute("IsDrop") == true then
local dropAmount = hit:GetAttribute("Amount")
hit:Destroy()
print("cool! drop detected")
player:WaitForChild("leaderstats"):WaitForChild("Cash").Value += dropAmount
end
end
end)
Correct, just a slight typo. player still wasn’t defined. To the op remember that .Touched only gives you the basepart that touched for free, not a player. If you are not sure use the anon auto-filled option which automatically fills in the correct arguments.
Okay I think I’ve figured out the problem. My collector part is in the workspace, hereby not able to access player. I can use GetNameFromUserIdAsync() to get it from my tycoon’s UserId attribute. Thanks for the help though!
In a Tycoon, you should add an object value to the Tycoon which holds which player owns it. You’ll need to use that in nearly every script going forward.
If you saved the userId you can as easily store the name, or better still an object value pointing at the player.
Okay, I’ve fixed it: I’m going to leave the final code here for anybody that wants to see it
local cPart = script.Parent
local players = game:GetService("Players")
local playerId = script.Parent.Parent.Parent.Parent:GetAttribute("UserId")
local player = players:GetNameFromUserIdAsync(playerId)
cPart.Touched:Connect(function(hit)
if hit:GetAttribute("IsDrop") == true then
wait(0.5)
local dropAmount = hit:GetAttribute("Amount")
hit:Destroy()
print("cool! drop detected")
players:WaitForChild(player):WaitForChild("leaderstats"):WaitForChild("Cash").Value += dropAmount
end
end)