I am making a "simulator" game and I want the player to step on a part, and if the player has over or equal to 100 "Cash" the player will teleport to "the desert", this is what i have tried
function onTouched(touch) if player.leaderstats.Cash.Value >= 100 then plr = touch.Parent:findFirstChild("Humanoid") if plr ~= nil then plr.Torso.CFrame = CFrame.new(-404, -12.5, 402) end end script.Parent.Touched:Connect(onTouched)
Your code was this:
function onTouched(touch) if player.leaderstats.Cash.Value >= 100 then plr = touch.Parent:findFirstChild("Humanoid") if plr ~= nil then plr.Torso.CFrame = CFrame.new(-404, -12.5, 402) end end script.Parent.Touched:Connect(onTouched)
The code is meant to be this:
script.Parent.Touched:Connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr.leaderstats.Cash.Value >= 100 then hum = hit.Parent:findFirstChild("Humanoid") if hum ~= nil then plr.Parent:MoveTo( --Your End Part ) end end end)
Firstly, I'd suggest making an end part (a part that the player will teleport to)
Secondly, the Humanoid
is in a player's Character
, not in the Player
itself!
Thirdly, you're picking up the "plr" completely out of the blue. You have to get the player, not just magically reference it!
Here's the fixed code for ya:
local endpart = game.Workspace:WaitForChild('endteleport') local startpart = script.Parent local coinsneeded = 100 startpart.Touched:Connect(function(touched) local char = touched.Parent local plr = game.Players:GetPlayerFromCharacter(char) local coins = plr.leaderstats.Coins.Value local humroot = char.HumanoidRootPart if coins >= coinsneeded then humroot.Position = endpart.Position print('Teleported '..plr.Name' successfully!') else if coins < coinsneeded then print(plr.Name..' does not have enough coins to enter the teleporter!') end end end)