Non-syntax error in my code.. any help?

I’m trying to make a leaderstat value go up when a function is called. The function that is being called can’t get the player, so there’s a function embedded in another function. I’m a bit new to scripting, and am wondering why this won’t work:

local count = script.Parent.Count

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "SpecialPinkBall" or  hit.Name == "GodPinkBall" or hit.Name == "SmokingCharcoal" or hit.Name == "BigSmokingCharcoal" or hit.Name == "SmokingGodBall" or hit.Name == "FlamingPinkCrate" or hit.Name == "FlamingSparklesPinkCrate" or hit.Name == "GiantBlueCrate" or hit.Name == "GiantGlowingBlueCrate" or hit.Name == "GiantGodlyYellowShinyBall" or hit.Name == "GodlyYellowShinyBall" or hit.Name == "TheBadGodlyCrate" or hit.Name == "TheGoodGodlyCrate" or hit.Name == "Stinky" or hit.Name == "Bad" or hit.Name == "SparklingNeonGreenBall" or hit.Name == "GiantNeonGreenBall" or hit.Name == "BadBall" or hit.Name == "SmokingBadBall" or hit.Name == "WhiteFireBlueBall" or hit.Name == "GodlyBlueBall" or hit.Name == "GiantGodlyBlueBall" or hit.Name == "GoodGodlyBlueBall" or hit.Name == "AGodlyBall" or hit.Name == "GiantGodlyInsaneGodBall" then
		script.Parent.BrickColor = BrickColor.new("Really red")
		wait(.5)
		script.Parent.BrickColor = BrickColor.new("Lime green")
		print("Boy")
		
		hit:Destroy()
		script.Parent.Success:Play()
		
		count.Value = count.Value + 1
		game.Workspace.PackageScreen.Sign.SurfaceGui.SIGN.Text = "Completed Packages: " .. count.Value
		
		game.Players.PlayerAdded:Connect(function(player)
		player.leaderstats.Packages.Value = player.leaderstats.Packages.Value + 1
		end)
	end
end)
3 Likes

Are you sure its being called?

2 Likes

It’s not being called. It’s in a serverscript

2 Likes

The event will only be fired when a player is added after it is set up.

2 Likes

I meant printed my bad so is it?

2 Likes

What do you think would be the optimal solution?

1 Like

Everything prints up until it gets to the 2nd function

1 Like

From the code, I’m assuming it is a singleplayer game, right?

1 Like

It’s not. Every player on the server should have the same stats affected by the same thing

1 Like

well does the script print “Boy”?

1 Like

yes it does print the word boy

1 Like

Ok then, so if you want it to increase the stat for every player, you can loop through all the players and increase it for each one.

You can loop through all the players using something like this:

for _, player in pairs(game.Players:GetPlayers()) do
    -- increase stat here
end

game.Players:GetPlayers() returns a table containing all the players in the game.

And if you’re wondering, the underscore is just the index for the value.

Example:

local randomTable = {
    ["cow"] = 1,
    ["hi"] = 5
}

for i, v in pairs(randomTable) do
    print(i)
    print(v)
end

-- output:
cow
1

hi
5
1 Like

So did you make it so it increases the players stat?

1 Like

my bad accidentally sent it lol (finished writing it)

1 Like


like this?

I think you messed up the typing a bit, it’s supposed to be written like this:

for _, player in pairs(game.Players:GetPlayers()) do
   player.leaderstats.Packages.Value = player.leaderstats.Packages.Value + 1
end

You could store the names in a table then use table.find to see if the name is in the table. This is way better to write and read than that eyesore (my vocabulary isn’t the best)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.