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
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:
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local GunNames = {
["Assault"] = true,
["Pistol"] = true,
["SMG"] = true,
["Shotgun"] = true,
["Sniper"] = true,
}
script.Parent.Touched:connect(function(part)
local Player = Players:GetPlayerFromCharacter(part.Parent)
if not Player then
return
end
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)
Hope this helped, let me know if you have any questions.
3 comments
{a = 1, b = 2, c = 3} == {['a'] = 1, ['b'] = 2, ['c'] = 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.
ScriptGuider thank you for explaining and helping / guiding me with the script it makes much more sense now :)