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

what does unable to cast value to object mean?

Unable to cast value to object on line 09.

Trying to make a gamepass door sort of thing so when you walk "through" the door, the game prompts a gamepass purchase of the gamepass i set in the script.gamepassid.value value, and then it will check to see if you own it and if you do then it will teleport you to the other side. if someone could help me with this then that would be awesome :D thanks

local hasgamepass = false
local function OnTouched(otherPart)
	if otherPart and otherPart.Parent and otherPart.Parent:FindFirstChild('Humanoid') then
		local player = PlayersService:GetPlayerFromCharacter(otherPart.Parent)
		if player and not JustTouched[player] then
			JustTouched[player] = time()
		end
		if hasgamepass == false then
			GamePassService:PromptGamePassPurchase(player.userId, script.GamePassId.Value)
		end
		if GamePassService:UserOwnsGamePassAsync(player.userId, script.GamePassId.Value) then
			TeleportToOtherSide(player.Character, otherPart)
			local hasgamepass = true
		end
	end
end

1 comment

0

Your problem is the first argument you passed "player.userId," is a number. You want a player object. Replace it with just player. https://wiki.roblox.com/index.php?title=API:Class/MarketplaceService

1 answer

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
	assert(parent, "ERROR: WaitForChild: parent is nil")
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end

-----------------
--| Variables |--
-----------------

local GamePassService = game:GetService('MarketplaceService')
local PlayersService = game:GetService('Players')

local VipDoor = script.Parent

local GamePassIdObject = WaitForChild(script, 'GamePassId')

local JustTouched = {}

-----------------
--| Functions |--
-----------------

-- Finds out which side the player is on and teleports them to the other
local function TeleportToOtherSide(character, hitPart)
	local bottomOfDoor = VipDoor.CFrame.p - Vector3.new(0, VipDoor.Size.Y / 2, 0)
	local inFrontOfDoor = bottomOfDoor + VipDoor.CFrame.lookVector * 3
	local behindDoor = bottomOfDoor - VipDoor.CFrame.lookVector * 3
	
	local distanceToFront = (inFrontOfDoor - hitPart.Position).magnitude
	local distanceToBack = (behindDoor - hitPart.Position).magnitude
	if distanceToFront < distanceToBack then
		character:MoveTo(behindDoor)
	else
		character:MoveTo(inFrontOfDoor)
	end
end


local hasgamepass = false
local function OnTouched(otherPart)
	if otherPart and otherPart.Parent and otherPart.Parent:FindFirstChild('Humanoid') then
		local player = PlayersService:GetPlayerFromCharacter(otherPart.Parent)
		if player and not JustTouched[player] then
			JustTouched[player] = time()
		end
		if hasgamepass == false then
			GamePassService:PromptGamePassPurchase(player.userId, script.GamePassId.Value)
		end
		if GamePassService:UserOwnsGamePassAsync(player.userId, script.GamePassId.Value) then
			TeleportToOtherSide(player.Character, otherPart)
			local hasgamepass = true
		end
	end
end

-- Removes old entries in JustTouched
local function RemoveOldTouches()
	for player, touchTime in pairs(JustTouched) do
		if time() > touchTime + 0.3 then
			JustTouched[player] = nil
		end
	end
end

--------------------
--| Script Logic |--
--------------------

VipDoor.Touched:connect(OnTouched)

while true do
	RemoveOldTouches()
	wait(1/30)
end

1 comment

0

What's with the whole defining WaitForChild? Is there something wrong with parent:WaitForChild(name)?

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