Script not going through if statement

Hi,

I’ve been trying to get my tool giver script to work for a while now. Long story short, I have a server script that check whether or not -inside of a CharacterAdded() function- the player owns a certain gamepass. If the player does, it should give a tool but it does’nt… I’ve tried print() to see if i could receive any data on where the if statement was, but literally nothing shows up. The weird part is that the script works only if its in a PlayerAdded() function, but not CharacterAdded().

Here’s my code:

players_service.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentTrowel"]) then
			local perm_trowel = game.ReplicatedStorage.Items:WaitForChild("Permanent Trowel"):Clone()
			perm_trowel.Parent = player.Backpack

			print("perma trowel given to "..player.Name)
		else
			print("no trowel")
		end

		if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentLandMine"]) then
			local perm_landmine = game.ReplicatedStorage.Items:WaitForChild("Permanent LandMine"):Clone()
			perm_landmine.Parent = player.Backpack

			print("perma landmine given to "..player.Name)
		else
			print("no landmine")
		end

		player.parameters.InSpawnZone.Value = false

		character:WaitForChild("Humanoid").Died:Connect(function()
			if workspace:FindFirstChild(player.Name.."'s Ball") then
				workspace:FindFirstChild(player.Name.."'s Ball"):Destroy()
			end
		end)
	end)
end)

Thanks!

5 Likes

I am not 100% sure, but I believe the issue is that you have the CharacterAdded command inside the PlayerAdded command, which means it will only run the code if the character AND player is loaded at the same time, which is very unlikely.

3 Likes

Where should I put the CharacterAdded() function then?

2 Likes

Here is a link to a post explaining your problem.

https://devforum.roblox.com/t/the-correct-way-to-do-characteradded-and-playeradded/3343708

3 Likes

I think the reason why CharacterAdded isn’t working/firing is because sometimes, especially when the first player joins the server, the character loads first before PlayerAdded fire.

It’s best to use the function connected to CharacterAdded as a separate script inside StarterPlayer.StarterCharacterScripts instead. In this way, the script will always run when the player’s character loads or resets.

3 Likes

Could I simply fire a RemoteEvent using a LocalScript inside of StarterCharacterScript and receive it inside of a separate ServerScript?

2 Likes

My question is why do we need CharacterAdded in the first place, tools are (typically) stored in the Player’s backpack, which the character isn’t needed for.

2 Likes

It’s because I want the tool to stay in the backpack even after death or resetting.

2 Likes

Alternatively, if you still need the PlayerAdded event, you could set a parameter called character and set it to player.Character or player.CharacterAdded:Wait(). This will get the player’s character if it already exists and if not, it can just wait for the CharacterAdded to fire then get the player’s character.

Using this method, the script would look like this:


players_service.PlayerAdded:Connect(function()
	local character = player.Character or player.CharacterAdded:Wait()
	if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentTrowel"]) then
		local perm_trowel = game.ReplicatedStorage.Items:WaitForChild("Permanent Trowel"):Clone()
		perm_trowel.Parent = player.Backpack

		print("perma trowel given to "..player.Name)
	else
		print("no trowel")
	end

	if market_service:UserOwnsGamePassAsync(player.UserId, gamepass_ids["PermanentLandMine"]) then
		local perm_landmine = game.ReplicatedStorage.Items:WaitForChild("Permanent LandMine"):Clone()
		perm_landmine.Parent = player.Backpack

		print("perma landmine given to "..player.Name)
	else
		print("no landmine")
	end

	player.parameters.InSpawnZone.Value = false

	character:WaitForChild("Humanoid").Died:Connect(function()
		if workspace:FindFirstChild(player.Name.."'s Ball") then
				workspace:FindFirstChild(player.Name.."'s Ball"):Destroy()
		end
	end)
end)

2 Likes

The thing is I don’t necessarily need the player’s character, I need the function (idk if you get what im trying to say)

2 Likes

In that case, just put the tool inside Player.StarterGear after giving the tool to the player’s backpack.

like this:

tool.Parent = player.Backpack
tool:Clone() = player.StarterGear
2 Likes

You could use the PlayerAdded function which fires when the player first joins the game and then a CharacterAdded function which fires every time the player respawns.

This probably isn’t the best way but it should work.

2 Likes

This is’nt what i’m looking for… I don’t want to seem picky but I don’t need the character itself, I need to check if the player owns the gamepass everytime the character loads so that even when the player resets, it still gives the tool.

2 Likes

Let me try that!

roblox character minimum

3 Likes

Sorry if I don’t fully understand the goal here, I haven’t fully read the script yet. Sorry for that

So let’s say a player owns a gamepass while playing the game. A little while later, the player removes the gamepass from your inventory. You want that after they removed it from their inventory, after they reset, the game doesn’t grant them the tool anymore. Right?

2 Likes

A player still owns a gamepass even if it is removed from their inventory.

1 Like

Oh.

char chara charac charact

1 Like

In some way, yes

But what i’m trying to achieve is a way for the player to receive the tools everytime their character loads / resets becaue otherwise, players lose their tools after dying. So my idea was to check if the player has the gamepass everytime their character loads so that they can receive their tools even after dying.

1 Like

I see. When the player joins the game, no need to use the CharacterAdded event. After checking if they own the gamepass, you can directly parent the tool into their backpack and then clone the tool and parent it to their StarterGear. The StarterGear lets the tools inside it to be automatically given to the player everytime they die or reset their character. Like I mentioned earlier.

For the Humanoid.Died event, you can simply use the character variable that I mentioned earlier.

2 Likes

That makes sense! I’ll try it and I’ll get back to you.

1 Like