BoolValue isn't getting detected

Hi, for the past few hours I’ve been trying to make a script that checks if you own a game pass and if you do, check if a mode is enabled. So far, everything’s working out except the script that checks if the mode is enabled. Here’s the script:

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	local humanoid = hit.Parent.Humanoid
	local PassId = 000 --- I put my game pass id here.
	if plr then
		
		local name = plr.Name
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, PassId) then
			
			if game.Players[plr.Name]:FindFirstChild("ez").Value == true then
			
			else
				
				humanoid.Health = 0 ---Kills you no matter what for some reason.
			end
			
		
		end
			
	end
end)

To show where the boolvalue is, here’s a screenshot:
Screen Shot 2020-10-26 at 12.13.00 PM
What’s also weird is, even though the value is true, it kills:
Screen Shot 2020-10-26 at 12.13.51 PM
I looked over the dev forum before posting, just in case someone found the solution before me. f you somehow know what the problem is, please let me know. Thanks.

2 Likes

Why are you indexing a player’s name in the Players service when you already called the player’s instance? Couldn’t you just simply use your variable that you defined plr?

2 Likes

Does the local player function indicate to the players in the game? I also tried using this:

if plr.ez.Value == true then

What’s really the issue here? Did you get any errors?

Also, using HumanoidObject.Health = 0, will, of course, kill the player, no matter what. You can use HumanoidObject:TakeDamage(#), which can’t do damage to a Humanoid, if it has ForceField.

Sorry, I might’ve confused you a little. The script is only supposed to kill if the value is false or you don’t own the game pass. It kills no matter what. Also, there’s no errors.

Try using if plr:WaitForChild("ez").Value == true then, since, firstly, you already have a PlayerObject, and secondly, we use :WaitForChild(), which will yield until it finds the instance with given name.

The only error I get now:
Screen Shot 2020-10-26 at 12.30.25 PM

Are you sure that error came from that script, and, mind showing the line which gives that error?

The part is being touched by a part in an accessory then, not a body part in the character.

I agree with this, no point in trying to index hit if you are going to do this.

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	local humanoid = plr and plr.Character and pl.Character:FindFirstChild("Humanoid")
	if not humanoid then return end

This would be a better and wouldn’t error if an accessory hit or something like that.

Like ijmod said, use the plr variable for this. Don’t bother using FindFirstChild if you aren’t going to check if it exists, just directly index.

Wrap this in pcall because it could error.

What should I wrap in a pcall function? The part where it checks if they own a game pass?

The only problem in the script is it’s not detecting the boolvalue, if that can be fixed, I’m pretty sure the whole script will be working.

you might wanna try this script out

script.Parent.Touched:Connect(function(hit)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
        if player:FindFirstChild("ez").Value == true then
        --your code here
    end
end)

Is the value being added on the client or server

1 Like

It’s changed by the server I believe

Yes, but what created it? Was it inserted by a local script or script?

Server script. I’ll be able to provide more details when I’m on pc. Plus, I don’t think you can change a value of a boolvalue in a local script.

1 Like

My bad, I read your post wrong, I’m indexing the player since I put the boolvalue inside the player service.

1 Like

have you tried something along the lines of:

local PassId = 000

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr then
		local name = plr.Name
        local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") -- find a humanoid instance in the player's character

		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, PassId) then
			local ez = plr:FindFirstChild("ez") -- find the boolvalue

			if ez then -- check to see if it's nil
			  if ez.Value then
                 -- code here
			  else
                  if humanoid then -- like FindFirstChild, check to see if the humanoid exists
				     humanoid.Health = 0
                  end
			  end
           end
		end	
	end
end)

I’ve made a huge mistake, i’ve been using a local script to change the value. How would I change the value from using a server script inside a GUI?

you can make a system for which when a player joins and they own a gamepass, it enables it (from the server)
or

local PassId = 000

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

	if plr then
		local name = plr.Name
        local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")

		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, PassId) then
			local ez = plr:FindFirstChild("ez") 

			if ez then
              if not ez.Value then ez.Value = true end -- enables it, if it isn't enabled

			  if ez.Value then
                 
			  else
                  if humanoid then
				     humanoid.Health = 0
                  end
			  end
           end
		end	
	end
end)