I am having a problem with this script. It is a Local Script. It is for my space game so when the client detects the player touches it the touched player gets teleported. But idk why it teleports all players. Please help.
local debounce = true
function onTouched(hit)
print(“Touched”)
if debounce == true then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent.Humanoid then
debounce = false
if player.Character.Destination.Value == 0 then
print("Player In Orbit")
wait(0.001)
debounce = true
elseif player.Character.Destination.Value == 1 then
game:GetService('TeleportService'):Teleport(5791169624,player)
wait(0.001)
debounce = true
elseif player.Character.Destination.Value == 2 then
game:GetService('TeleportService'):Teleport(5820166010,player)
wait(0.001)
debounce = true
end
end
Please put ```at the start and end of your script, so it’s cleaner for us to read.
You gotta check if the player who touches the thing is the local player.
a) Instead change this to a server script (will cause delay on touch).
b) Add at the start: local cPlayer = game:GetService("Players").LocalPlayer and add an if statement which checks if the player is the same as the client player (cPlayer)
if player == cPlayer then
I suggest instead of checking if hit.Parent.Humanoid then doing:
I just have a problem with the ServerScript since i have a debounce script to prevent lag. When more players are sitting in the rocket some of the players dont get teleported because of the delay/debounce this is a problem
-- because it's client sided you won't need a debounce as the player will be teleported already
local cPlayer = game:GetService("Players").LocalPlayer
game.Workspace.ReachOrbit.Touched:Connect(function(hit)
print(“Touched”)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and player == cPlayer then
if player.Character.Destination.Value == 0 then
print("Player In Orbit")
elseif player.Character.Destination.Value == 1 then
game:GetService('TeleportService'):Teleport(5791169624,player)
elseif player.Character.Destination.Value == 2 then
game:GetService('TeleportService'):Teleport(5820166010,player)
-- wait() this is the same as wait(0.01)
end
end
end)
--[[ I only suggest using this: game.Workspace.ReachOrbit.Touched:Connect(onTouched)
when you want to use a function twice, instead connect it directly to it.
The "end" of the statement needs a ) behind it; so it'd be end) when you connect it directly. ]]