Well, i need some help if anyone can. I've been trying for the past 30 minutes to get this working but it doesn't. I've been trying to make a script where when a player touches a part, the script takes a leader stat value from the player and multiples it by a certain value (like 1 or 4) and gives them the amount calculated.
So far I've come up with this
local player = game.Players local Players = game.Players local G = player:FindFirstChild("leaderstats").leaderstats:FindFirstChild("Grass").Value local total = (G * 1) script.Parent.Touched:Connect(function(hit) workspace.RemoteEvent:FireServer(player.Name) if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Grass") then player.leaderstats.Grass.Value = player.leaderstats.Grass.Value + total end end)
Although I've tried this many times i had no choice but to ask for help, in this script sofar Line 3 is showing "Attempt to Index a nil value" If anyone could help me i would be so thankful.
You're not targeting the actual Player, instead, you're targeting the Service that handles all the Players. This means you're trying to access a Folder within a Service meant to be a descendant of Child. Consider changing line 1 to
local Player = game:GetService("Players").LocalPlayer --// would be defined with '.' local Grass = Player:WaitForChild("leaderstats").Grass Grass.Value = (Grass.Value * 1)
If you're trying to go through every player, and this Isn't a LocalScript, consider using a pairs loop to Iterate through all the Players. There are a few other functions too like next,
for example, check those out while you're at it
local Players = game:GetService("Players"):GetPlayers() for p in ipairs(Players) do --// ipairs may be a bit confusing, if so, work with pairs instead. local Grass = Players[p]:WaitForChild("leaderstats").Grass Grass.Value = (Grass.Value * 1) end
I also noticed you passed player.Name
as an argument towards the Server, this is automatically done, therefore it is unneeded, don't forget to reference the Player Variable through first when listening for the Event.
Hope this helps! don't forget to accept and upvote!