Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Door wont open when touched?

script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild('Humanoid') then local plr = hit.Parent:FindFirstChild('Humanoid') if plr.Parent.leaderstats.kills >= 1 then script.Parent.Transparency = 1 script.Parent.CanCollide = false wait (0.5) script.Parent.CanCollide = true script.Parent.Transparency = 0 else print "ha" end end end)

error:

16:43:41.278 - Workspace.PaidA.Script:4: attempt to compare number with userdata 16:43:41.279 - Stack Begin 16:43:41.280 - Script 'Workspace.PaidA.Script', Line 4

1 answer

Make sure you put your code in a code block next time.

This is your script.

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		local plr = hit.Parent:FindFirstChild('Humanoid')
		if plr.Parent.leaderstats.kills >= 1 then		-- This is your error.
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
			wait (0.5)
			script.Parent.CanCollide = true
			script.Parent.Transparency = 0
		else
			print("ha")
		end
	end
end)

Assuming leaderstats is a model and kills is a NumberValue, what you're basically outlining is this:

if plr.Parent.[Model].[NumberObject] >= 1 then

You're comparing literally an object to a number. What you might be intending to do to wipe away the error is comparing its value.

Therefore:

if plr.Parent.leaderstats.kills.Value >= 1 then

I detected a potential logic error in this script. A better way to make sure that it is the player it is detecting, not a random NPC (if you do have NPCs, or any model(s) with a Humanoid in it), you can use the :GetPlayerFromCharacter() method. If you do not have any models with a Humanoid in it, then by all means skip this.

The :GetPlayerFromCharacter() method literally gets the player from the character object.

The formula goes like this:

game.Players:GetPlayerFromCharacter(--[[ Insert valid character object here ]])

EXAMPLE

script.Parent.Touched:connect(function (hit)
	local Human = hit and hit.Parent and game.Players:GetPlayerFromCharacter(hit) and hit.Parent:FindFirstChild("Humanoid").Health > 0 and hit.Parent:FindFirstChild("Humanoid")
	if Human then
		print("Player detected. The Player's name is ".. game.Players:GetPlayerFromCharacter(hit.Parent).Name)
	end
end)

I added the Humanoid's health bit just in case if the body part themselves are touching it, but the character died.

Also, double check if the leaderstats model is where it's supposed to be. By convention (in my terms), it is located in the Player model.

game.Players.LocalPlayer.leaderstats

This discussion is preserved as it was. New answers, comments, and votes are closed.