Because you cannot fire the client without a player object already existing, line 36 does not work. The intended effect is that it will fire the client, check if the player is within a certain range of the item, and then fire the server to add it to the backpack. I cannot think of another way to structure this to make it work in this way. Anyone got any ideas?
#Accessing a player variable in a server script - Item Spawner
12 messages · Page 1 of 1 (latest)
I think one way would be to have the click detector pre-added onto the items as a starting point but that still doesnt fix the root issue

I had no idea this existed
Thank you
local rand = math.random(1,v.MaxTimer.Value)
coroutine.wrap(function()
task.wait(rand)
-- ...
end)()
Instead of the above, use task.delay:
local randomTime = math.random(item.MaxSpawnInterval)
task.delay(randomTime, function()
-- ...
end)
Also, you can rid of findIngredientByName in favour of degradingingredients into the instance itself and using FindFirstChild
And to promote readability, you can pull back on nesting by inverting your if statements
for _, spawn in ingredientSpawns do
if spawn.Taken.Value then
continue
end
local ingredientName = spawn.IngredientName.Value
local ingredientPrefab = ingredientPrefabs:FindFirstChild(ingredientName)
if not ingredientPrefab then
warn("Could not find ingredient \"" .. ingredientName .. "\"")
continue
end
local randomTime = math.random(ingredient.MaxSpawnInterval.Value)
task.delay(randomTime, function()
local ingredient = ingredientPrefab:Clone()
ingredient:PivotTo(spawn:GetPivot()
ingredient.Parent = workspace
ingredient.ClickDetector.MouseClick:Once(function(player)
-- ...
end)
end)
end
It's best not to start a new thread until you're sure the ingredient exists
@supple tinsel