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

Expect <eof> near end explanation / help?

So i have this script to give tools on touch (below)

function onTouched(part)
        p = game.Players:playerFromCharacter(part.Parent)
        if p == nil then return end
	gun1 = game.Lighting.Assault:clone() 
        if p.Backpack:findFirstChild(gun1.Name) ~= nil then return end
        gun1.Parent = p.Backpack
	gun2 = game.Lighting.Pistol:clone() 
        if p.Backpack:findFirstChild(gun2.Name) ~= nil then return end
        gun2.Parent = p.Backpack
	gun3 = game.Lighting.SMG:clone() 
        if p.Backpack:findFirstChild(gun3.Name) ~= nil then return end
        gun3.Parent = p.Backpack
	gun4 = game.Lighting.Shotgun:clone() 
        if p.Backpack:findFirstChild(gun4.Name) ~= nil then return end
        gun4.Parent = p.Backpack
	gun5 = game.Lighting.Sniper:clone() 
        if p.Backpack:findFirstChild(gun5.Name) ~= nil then return end
        gun5.Parent = p.Backpack
				
end
end
		end
	end
end}

script.Parent.Touched:connect(onTouched)

there is an error on the script and it says

Expected near end can some answer / explain what that means?

2 answers

✓ Accepted answer

Error

It's nice knowing exactly what your error message means, so you can begin debugging your program. I'll start by stating that eof stands for end of file, which has to do with ending a scope you're working with.


For future reference, I strongly advise you to do a lot of research on the error messages you get, so you can do a lot better job of learning from mistakes (and if you have trouble understanding some, just ask!)

Scope

A "scope" is basically any block of code that requires an end statement. You need to make sure that you have an end statement for each scope, or your program will break in a syntax error.

Syntax error

A syntax error is when a problem occurs in your program involving the way it's written. Syntax is what makes all programming languages unique, and different from one another.


You also have another syntax error besides just using too many end statements, which is an arbitrary closing curly bracket } (at the end of your last end statement on line 24). You're not working with any table values, and there are no opening curly brackets to complete it ( { ). This will also break your program since it goes against the rules of how this language can be written

Solution

Knowing everything I just covered, perhaps you could study these topics a bit more and practice understanding this solution:

function onTouched(part)
        p = game.Players:playerFromCharacter(part.Parent)
        if p == nil then return end
    gun1 = game.Lighting.Assault:clone() 
        if p.Backpack:findFirstChild(gun1.Name) ~= nil then return end
        gun1.Parent = p.Backpack
    gun2 = game.Lighting.Pistol:clone() 
        if p.Backpack:findFirstChild(gun2.Name) ~= nil then return end
        gun2.Parent = p.Backpack
    gun3 = game.Lighting.SMG:clone() 
        if p.Backpack:findFirstChild(gun3.Name) ~= nil then return end
        gun3.Parent = p.Backpack
    gun4 = game.Lighting.Shotgun:clone() 
        if p.Backpack:findFirstChild(gun4.Name) ~= nil then return end
        gun4.Parent = p.Backpack
    gun5 = game.Lighting.Sniper:clone() 
        if p.Backpack:findFirstChild(gun5.Name) ~= nil then return end
        gun5.Parent = p.Backpack
end -- We only need one end statement, which closes the "onTouched" function. All the other if statements above have their end statement, so we don't need any additional ones.

script.Parent.Touched:connect(onTouched)

A better solution

The code above is a solution to your provided code with little changes made. But we can actually clean this code up a bit, to make it easier for us and others to read, along with improving some efficiency for the program:

-- Defining constants at the beginning of your code
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage") -- I STRONGLY recommend using either ServerStorage, or ReplicatedStorage INSTEAD of the Lighting service to STORE any items.

-- Using a dictionary table of gun names to prevent the need for if statements checking each condition. We'll use a generic for loop to iterate this
local GunNames = {
	["Assault"] = true,
	["Pistol"] = true,
	["SMG"] = true,
	["Shotgun"] = true,
	["Sniper"] = true,
}

script.Parent.Touched:connect(function(part) -- We can just use an anonymous function and connect it directly, instead of defining one.

	-- Using "GetPlayerFromCharacter" instead of "playerFromCharacter" which is invalid (that's not a syntax error, but no such method exists so it will end up breaking anyway)
	local Player = Players:GetPlayerFromCharacter(part.Parent)

	-- Readability is very important
	-- I personally like using the "not" inverter instead of ~= nil, but this is purely just personal preference.
       if not Player then
		return
	end
	
	-- Using a generic for loop to iterate the ServerStorage for the guns that are in the "GunNames" dictionary
	for i,v in next, ServerStorage:GetChildren() do
		if GunNames[v.Name] and not Player.Backpack:FindFirstChild(v.Name) then
			v:Clone().Parent = Player.Backpack
		end
	end

end) -- Remember to put a closing parenthesis here if you are to use this method however, since our anonymous function is an argument, it's still being enclosed in the parameters of the connect method.

Hope this helped, let me know if you have any questions.

3 comments

0

{a = 1, b = 2, c = 3} == {['a'] = 1, ['b'] = 2, ['c'] = 3}

3

Yeah, I just like using brackets most of the time when working with dictionary format, just to see the nice string syntax highlighting of my keys.

0

ScriptGuider thank you for explaining and helping / guiding me with the script it makes much more sense now :)

eof stands for "end of line". meaning that the lua interpreter (which compiles and runs your source code for you), thinks you need to use a new line (by pressing enter). After looking at your source code whoever, I do not think that is the case (the interpreter can be wrong if it experiences an error).

The first thing I noticed was end} which does not make sense (since your not putting the function into a table). You also have several end statements that do not belong to any statement.

So the end of your function (directly after the last gun5.Parent = p.Backpack ) would look like this:

		gun5.Parent = p.Backpack
end

script.Parent.Touched:connect(onTouched)

1 comment

0

Too late for my answer. Use the answer above. It's a lot better.

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