Works in studio but not in game?

This works perfectly in studio but does not work in-game. Only the XP part.

Here is the section of the script where I’ve added the XP.

OP
players = game.Players:getChildren()
	local survivedt = {}
	for i=1,#players do
		local survived = getTag(players[i], "BoolValue", "survived", false)
		if survived then
			setTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Survivals", getTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Survivals", 0) + 1)
			setTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Coins", getTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Coins", 0) + 10)
			setTag(players[i], "IntValue", "EXP", getTag(players[i], "IntValue", "EXP", 0) + 30)
			table.insert(survivedt, players[i])
		end
		if not survived then
			setTag(players[i], "IntValue", "EXP", getTag(players[i], "IntValue", "EXP", 0) + 15)
		end
	end
PlayerCompiler
function compilePlayers(players)
	local names = ""
	if #players == 1 then return players[1].Name end
	for i=1,#players do
		if i == #players then
			names = names.. "and ".. players[i].Name
		else
			names = names.. players[i].Name.. ", "
		end
	end
	return names
end
setTag
function setTag(parent, type, name, value)

	local tag = parent:findFirstChild(name)
	if tag ~= nil then
		if tag.className == type then
			tag.Value = value
		else
			local newTag = Instance.new(type)
			newTag.Name = name
			newTag.Value = value
			newTag.Parent = parent 
		end
	else
		local newTag = Instance.new(type)
		newTag.Name = name
		newTag.Value = value
		newTag.Parent = parent 
	end
end
getTag
function getTag(parent, type, name, default)

	local tag = parent:findFirstChild(name)
	if tag ~= nil then
		if tag.className == type then
			return tag.Value
		else
			print("No tag of the specified name and class was found in ", parent)
			return default
		end
	else
		print("No tag named ", name, " found in ", parent)
		return default
	end
end

can you specify the problem? What doesn’t work?

This works in studio, as in while I’m testing, the EXP is deposited into the player’s XP value.

In game, it doesn’t do that at all

is there any errors that come out?

There are absolutely no errors

Try printing survived after you attempt your result. What does it output? Is it returning true or false? Maybe it’s nil. Thats what i suspect. Cant be true or false if it’s nil :wink:

1 Like

It comes out as ‘true’…
Maybe I need to write a code if survived then xp = xp + 30?
not exactly that but would it work something like that

I have a lot of suggestions/constructive critisism for your post/code.

  1. Your if statement can be merged into an if else statement. That makes code easier to read and the survived variable doesn’t have to be accessed a second time if it’s true.
  2. Although this isn’t that big of a deal since the function referenced is the exact same, you’re using deprecated capitalization. If you know of deprecations you should try really hard to avoid using deprecated functions/features as best you can even if you don’t know why. The capitalization you use makes code harder to read for some people and it can look a little weird when you’re looking for a capital letter there. (findFirstChild should be FindFirstChild)
  3. We don’t have the code for setTag/getTag which you rely on for the broken functionality. How can we know what’s going wrong if we pretty much only have some pseudocode?
  4. What does the players table consist of? How are you populating the players table? Are you updating the players table correctly when a player joins if that’s needed? The fact that it works in studio but not in game could be due to many reasons but usually this has to do with players since loading a player in studio is near instantaneous but in game it can take upwards of 5-10 seconds.
  5. You don’t use the i index so it would make a lot of sense to use ipairs to iterate over your players table. This makes your code easier to read and can offer some performance/reliability benefits.
1 Like

I’ll post the setTag and getTag functions soon.

I doubt that it is the player taking 5-10 seconds to load in because the EXP, survivals and coins aren’t rewarded until you completw the round.

Awesome! But make sure the table is being updated! If the player loads too late and you don’t update the table it’ll never have them in the table. I’ll edit this reply in a second with more stuff once you post your code.

I have put in playerCompiler, setTag, and getTag functions.