Cant give eggs problem

hi i made a collect eggs system and is show me errors here is the script:
script.Parent.Touched:Connect(function(hit)

local player = game.Players:GetCharacterFromPlayer(hit.Parent)

player.Collect.Eggs.Value = player.Collect.Eggs.Value + 1

end)

and the error:
image

If you look at the error, GetCharacterFromPlayer is not a member of Players. I think you have to say game.Players.Touched…etc

1 Like

but if i am change that so is cant change the eggs amount for the player: image

Well i know you are missing something between Players and GetCharacterFromPlayer. I am not an expert on scripting, but maybe mess around with words from the drop-down selection.

1 Like

It’s because you’re using GetCharacterFromPlayer instead of GetPlayerFromCharacter.

This code should work:

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)

player.Collect.Eggs.Value = player.Collect.Eggs.Value + 1
end)

To build on that, unless you want your output to be spammed with random errors you should probably add an if statement.

script.Parent.Touched:Connect(function(hit)
  local player = hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent)
  if player and player:FindFirstChild("Collect") and player.Collect:FindFirstChild("Eggs") then
    player.Collect.Eggs.Value = player.Collect.Eggs.Value + 1
  end
end)
2 Likes

thank you for all the help @TheGuyWithAShortName .