How to make a teleporter that verifies if a value is right?

Hi guys, I am trying to make a script that teleports you if you have 1 win or more. I tried a bunch of combinations but none of them seemed to have worked. I tried using a RemoteEvent etc… Here is what I’ve come up with so far:

script.Parent.Touched:Connect(function(player, h)
	local hum = h.Parent:FindFirstChild("Humanoid")
	if hum ~= nil and player.leaderstats.Wins <= 0 then
		h.Parent.HumanoidRootPart.CFrame = CFrame.new(-51, 83.5, -154)
	end
end)

the player is not a parameter in a touched event, so I would get rid of that first variable. Only the object that has touched the script’s parent will be the parameter. if you were to try and check the player’s Wins value you would have to use the GetPlayerFromCharacter() function.

if hum then
    local player = game.Players:GetPlayerFromCharacter(h.Parent)
    if player then -- checks to see if they are a real player
        -- your code here
    end
end

This doesn’t work. It outputs an error that says: "Attempt to index with nil:

Sorry for the late response, my WiFi had problems…

which line does this error occur, this line should be checking to see if the player is real before checking their wins value stats.

Edit: Also did you delete the first variable “player” in your event

This occurs Line 2:

script.Parent.Touched:Connect(function(h)
	local hum = h.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(h.Parent)
	if hum ~= nil and player.leaderstats.Wins <= 0 then
		h.Parent.HumanoidRootPart.CFrame = CFrame.new(-51, 83.5, -154)
	end
end)

Nevermind now it says there is an error on Line 4: [Workspace.WinnersTeleport.TeleportPart.WinnersDoor:4: attempt to compare userdata and number]

I think this is coz u tried to find humanoid before it loaded in, maybe try changing line 2 to
local hum = h.Parent:WaitForChild(“Humanoid”)
try adding a .Value behind wins

That’s because player.leaderstats.Wins is refering to the IntValue Object, and not its player.leaderstats.Wins.Value. Replace it with that.

By the time the touched event fired, the character surely loaded in.

Nope, now its an Infinite Yield because of WaitForChild

Even with this, the script won’t work:

script.Parent.Touched:Connect(function(h)
	local hum = h.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(h.Parent)
	if hum ~= nil and player.leaderstats.Wins.Value <= 0 then
		h.Parent.HumanoidRootPart.CFrame = CFrame.new(-51, 83.5, -154)
	end
end) 

Thanks for your reminder. And about local player = game.Players.LocalPlayer this is NOT a Local Script…

1 Like

Yeah, I accidentally made that post. I tried to delete it but my pc lagged, sorry for the confusion.

1 Like

Ok, do you have any other solutions that might work?

local script code

local rp = game:GetService("ReplicatedStorage")
local debounce = false
workspace.YOURTELEPORTER.Touched:Connect(function()
     if debounce == false then
        debounce = true
        rp.YOURREMOTE:FireServer()

        wait(5)
        debounce = false
     end
end

server script

local rp = game:GetService("ReplicatedStorage")

rp.YOURREMOTE.OnServerEvent:Connect(function(player)
      if player.leaderstats.Wins.Value <0 then
            player.Character.HumanoidRootPart.CFrame = CFrame.new(-51, 83.5, -154)
      end
end

not much else to say, pretty easy.
also sorry if the spacing is bad, just made it here on devforum

edit: added debounce

I think it should work, but it doesn’t, and the output doesn’t say anything.

you replace YOURREMOTE and YOURTELEPORTER?
also make sure if any symbols are incorrect, i know some devforum symbols are different than roblox studio symbols, just as “”

oh, so it worked. if so mark as solution

Nevermind, it didn’t work… I did the symbols correctly too.

let me test it out, if it isnt teleporting, it may be a issue with the cframe maybe add some print statements

improper end structure, here is updated server code

local rp = game:GetService("ReplicatedStorage")

rp.YOURREMOTE.OnServerEvent:Connect(function(player)
      if player.leaderstats.Wins.Value <0 then
            player.Character.HumanoidRootPart.CFrame = CFrame.new(-51, 83.5, -154)
      end
end)

Alright, I added prints, and NONE of them printed, which means that the LocalScript doesn’t detect that the part was touched()

Edit: I am SURE that i referenced the right part. That part is a Union btw (idk if this is important)