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

Why is this simple forcefeild script not working? It gives the output "'then' expected near <eof>"

I am trying to get a forcefeild for all players in one room, and then turn it off when they leave that room. This script is attached to the floor of the room, I tried it in two different ways:

if script.Parent.Touched:connect(function(hit)
	if hit.Parent.Humanoid then
		game.ReplicatedStorage.ForceField:Clone().Parent = hit.Parent
	end
end)

(I put a 'ForceFeild' in ReplicatedStorage)

if script.Parent.Touched:connect(function(hit)
	if hit.Parent.Humanoid then
		Instance.new("ForceFeild").Parent = hit.Parent
	end
end)

Does anyone know why it is not working?

4 comments

0

try putting .Parent in another lines

0

Delete the “if” on line 1 in both scripts.

0

Thank you Denny that was a stupid mistake I don't know why I did that

0

And I just realized that I spelled forcefield wrong

3 answers

if script.Parent.Touched:connect(function(hit)
    if hit.Parent.Humanoid then
        Instance.new("ForceField").Parent = hit.Parent
    end
end)

Do that, you misspelled "ForceField" you put "ForceFeild"

You have multiple errors.

  1. Remove the "if" on the first line of code.

  2. You're using :connect when it's :Connect.

  3. In your second line, you didn't add a :FindFirstChild.

  4. You misspelt "ForceField"

I will rewrite your code, working, no deprecated code, no errors.

-- I also added so you can
script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild"Humanoid" then
        if not part.Parent:FindFirstChildOfClass"ForceField" then
            local ff = Instance.new("ForceField",part.Parent)
            -- Done
        end
    end 
end)

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