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

Scripts not working/executing in both studio and roblox?

I added this script to my game but it's not working.

Script 1: Teleports player to another game

local TeleportService = game:GetService("TeleportService")
local gameID = 3262304056

function onTouched(hit)
	local player = game.Players.GetPlayerFromCharacter(hit.parent)
	if player then
		TeleportService:Teleport(gameID, player)
	end
end


script.Parent.Touched:connect(onTouched)

Script 2: Kills the player when touched

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
	end
end)

No errors show up and i tested the scripts by playing in both studio and regular roblox. I tried putting everything in capitals when needed.

1 answer

✓ Accepted answer

I'm NOT to sure about this but I think it's a problem with your if statements.

So in here you used the :GetPlayerFromCharacter() method/function... well you forgot to put a colon.

This is the correct verison of your first script i think:

local TeleportService = game:GetService("TeleportService")
local gameID = 3262304056

function onTouched(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.parent)
    if player then
        TeleportService:Teleport(gameID, player)
    end
end


script.Parent.Touched:connect(onTouched)

Next your kill-brick script has alot of unesscary conditions For example,

if hit and

This is just saying if hit exists, for the TouchedEvent to run it needs the hit parameter to exist. Why would you need to say if hit exists when the function is already running lol

Next condition is

and hit.Parent

This is saying if hit has an parent, anything that activates the touched event must have at least one parent The workspace!

To shorten your conditions use this instead:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid.Health = 0
    end
end)

:FindFirstChild() returns true or false if the given parameter DOES exist in the parent that used the method. So if a part drops down and activates the Touched Event, :FindFirstChild() would see if a humanoid existed, which there is no humanoid, then it would return false. Which the if statement sees and closes itself.

You can ask clarification anytime.

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