It wont work. The error specifically states that OP is indexing “nil” with leaderstats, which means “player” is nil. We have to understand the context in order to figure out what is going wrong.
wow, ok i think im caught up! did u use gnome codes tutorial? (seems random but bc i followed it and im making a clicking game i think i could help.) if so i can send u my code (his is kind of bad ngl)
Send us the full script. (I’ve asked like so many times :|||||)
The OP did omit the player argument from the connection at one point, so I’m not sure if that’s the reason or if OP added it back.
Exactly why I’m asking for the script OP is using
- My multiplier is 1
- It’s sensing the clicks now, the error pops up upon clicking
- I’ve only tested the game in Roblox Studio
- Thanks, @xvortex_portals
- Here’s the full clickReceiver script now:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local clickEvent = game.ReplicatedStorage.remoteEvents.click
clickEvent.OnServerEvent:Connect(function()
local leaderstats = player:WaitForChild("leaderstats")
local multiplier = player:WaitForChild("multiplier")
local clicks = player:FindFirstDescendant("clicks",true)
print("+1")
clicks.Value = clicks.Value + (multiplier.Value)
leaderstats.Clicks.Value = clicks
end)
But now the error is “attempt to index nil with ‘WaitForChild’” instead of “with ‘leaderstats’”
clickEvent.OnServerEvent:Connect(function(player) -- readd player
You omitted player, again
Remove this line: local player = Players.LocalPlayer
I’ve deleted the line, but now the clicks still aren’t going up
Did you re-add the player argument in the connection?
Actually, here’s the script.
local Players = game:GetService("Players")
local clickEvent = game.ReplicatedStorage.remoteEvents.click
clickEvent.OnServerEvent:Connect(function(player) -- add player argument
local leaderstats = player:WaitForChild("leaderstats")
local multiplier = player:WaitForChild("multiplier")
local clicks = player:FindFirstChild("Clicks",true) -- FindFirstDescendant is not enabled
print("+1")
clicks.Value = clicks.Value + (multiplier.Value)
-- clicks is a reference to leaderstats.Clicks!
end)
aright im going to bed cya tmrw
local Players = game:GetService("Players")
local clickEvent = game.ReplicatedStorage.remoteEvents.click
clickEvent.OnServerEvent:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local multiplier = player:WaitForChild("multiplier")
local clicks = player:FindFirstDescendant("clicks",true)
print("+1")
clicks.Value = clicks.Value + (multiplier.Value)
end)
I’m getting "infinite yield possible on ‘Players.(playername):WaitForChild(“multiplier”)’
YES that’s right i used gnome code for my tower defense, so we are using the same script. one second
ive got to go to bed but if u haven’t fixed it by around 4:00 tmrw [EST] i think ill be back on
Is the multiplier object being created, or is it supposed to be capitalized? WaitForChild is case-sensitive, so is just about every other method.
The multiplier is never created, it has its own script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
local multiplier = Instance.new("IntValue",player)
multiplier.Name = "Multiplier"
multiplier.Parent = player
multiplier.Value = 1
end)
I also just realized I don’t need leaderstats if I’m not gonna use it
You can remove multiplier.Parent
, because you’re already setting its parent in Instance.new("IntValue",player)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
local multiplier = Instance.new("IntValue",player)
multiplier.Name = "Multiplier"
-- remove multiplier.Parent in favor of doing it in the Instance.new function
multiplier.Value = 1
end)