Teleport portal

Im making a portal, you must have 100 coins to go through it, no errors pop up

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		if game:GetService("Players")[hit.Parent.Name].leaderstats.Coins.Value >= 100 then
			game:GetService("ReplicatedStorage").checkData:FireClient(game:GetService("Players")[hit.Parent.Name], "Portal1")
			
			
		end
	end
end)

starter player scripts

game:GetService("ReplicatedStorage").checkData.OnClientEvent:Connect(function(portal)
	if portal == "Portal1" then
		if game:GetService("Workspace").Portal1.Main:FindFirstChild("Info") then
			if game:GetService("Workspace").Portal1.Main.CanCollide == true then
				game:GetService("Workspace").Portal1.Main.CanCollide = false 
				game:GetService("Workspace").Portal1.Main.Info:Destroy()
			end
		end
	end
end)
1 Like

Try instead of checking in your first script if hit.Name == “HumanoidRootPart”, check if :GetPlayerFromCharacter(Hit.Parent) is not false.

Also, side note, good job using :GetService() for thing like Players as opposed to the . syntax. Lots of people don’t bother to but it’s good practice regardless.

You can also simplify game:GetService(“Workspace”) to just one word, workspace, lowercase.

3 Likes

Doesnt work still… i tried what you asked

Can you send your updated code?

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "HumanoidRootPart" then
		if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) == false then
			game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) = true
		end
		if game:GetService("Players")[hit.Parent.Name].leaderstats.Coins.Value >= 100 then
			game:GetService("ReplicatedStorage").checkData:FireClient(game:GetService("Players")[hit.Parent.Name], "Portal1")
			
			
		end
	end
end)

i prob did it wrong

Close, you just need to check it once at the top since the function returns true or false based on whether or not the instance is a Player’s character>

script.Parent.Touched:Connect(function(hit)
	if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
		if game:GetService("Players")[hit.Parent.Name].leaderstats.Coins.Value >= 100 then
			game:GetService("ReplicatedStorage").checkData:FireClient(game:GetService("Players")[hit.Parent.Name], "Portal1")
		end
	end
end)

Thanks that, still isnt working tho

Put prints under your if statements on the client sided part of things to see where it stops. That will show what line is preventing it from working.

1 Like

Usually for touched events I do this:
if hit.Parent:FindFirstChild('HumanoidRootPart') ~= nil then

You want to check if HRP is present. You basically checked if the player’s name is HumanoidRootPart

Edit:

if hit.Parent:FindFirstChild('HumanoidRootPart') ~= nil then
   local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- variable for the player
   if plr.leaderstats:WaitForChild('Coins').Value >= 100 then
      -- code
   end
end
1 Like

Still doesnt work… I did it correct

Its the toutch im pretty sure.

function doorTouched(hit)
   if hit.Parent:FindFirstChild('HumanoidRootPart') ~= nil then
      local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- variable for the player
      if plr.leaderstats:WaitForChild('Coins').Value >= 100 then
         local event = game.ReplicatedStorage:WaitForChild('checkData')
         event:FireClient(plr,'Portal1')
         print('event fired') -- just to check if the code ran correctly
      end
   end
end

script.Parent.Touched:Connect(doorTouched)

Are you sure you are setting your coins to 100 before testing? Put a print inside where you check if the player has 100 coins.

So it should look something like this:

if plr.leaderstats:WaitForChild('Coins').Value >= 100 then
      -- code
      print("player has 100 coins")
end

Then tell me if there is anything printed.

Yes I have 100 coins its in the leaderstats

Some odd reason its not working, maybe its the other script?

Can you send a video showing the script, then playtesting? Or if the file is too large, just show when you are playtesting

Did it print this?

If it did, then yes the issue is with your client script

I just walk into it and nothing happens, no errors.

No, it didn’t print that in the script