im trying to make a game where if you touch a part a zombie spawns. and then you need to kill that zombie by making it walk into a killbrick, and if it dies of the killbrick it will automaticly put a sword in your inventory.
problem 1: Workspace.DevForumWanu10.LocalScript:12: attempt to index nil with ‘Parent’
i get this error
problem 2: zombie wont spawn.
local Killer = game.Workspace.Killer --- its a kill brick
local Trigger = game.Workspace.Trigger --- part that causes zombie to spawn when touched
local Tam = game.ServerStorage:FindFirstChild("Tam") --- its the sword
local Zomb = game.ServerStorage:FindFirstChild("Zomb") ---- its a zombie
local BackPack = game.Players.LocalPlayer:FindFirstChild("BackPack") --- backpack of player
Killer.Touched:Connect(function(touchPart) --- checking if the part is touched
local humanoid = touchPart.Parent:FindFirstChild("Humanoid") ---checking if touchPart has a humanoid
if humanoid then --- if humanoid then make the zombie spawn in workspace
Zomb.Parent = workspace
if humanoid then --- if it has a humanoid its kill it
humanoid.Health = 0
if humanoid.Health == 0 then --- if the zombie is dead you get a reward/sword
Tam.Parent = game.Players.LocalPlayer.Backpack
end
end
end
end)
i have been debugging this for hours and nothing works.
The error is saying that it can’t find anything in serverstorage called “Zomb”. I think the reason this might be is because you make it a variable as soon as the server is created and script runs faster than the zombie model can be created. If you move the Zomb variable to the inside of the touched function it should work. Like this
tlocal Killer = game.Workspace.Killer --- its a kill brick
local Trigger = game.Workspace.Trigger --- part that causes zombie to spawn when touched
local Tam = game.ServerStorage:FindFirstChild("Tam") --- its the sword
local BackPack = game.Players.LocalPlayer:FindFirstChild("BackPack") --- backpack of player
Killer.Touched:Connect(function(touchPart) --- checking if the part is touched
local humanoid = touchPart.Parent:FindFirstChild("Humanoid") ---checking if touchPart has a humanoid
local Zomb = game.ServerStorage:FindFirstChild("Zomb") ---- its a zombie
if humanoid then --- if humanoid then make the zombie spawn in workspace
Zomb.Parent = workspace
if humanoid then --- if it has a humanoid its kill it
humanoid.Health = 0
if humanoid.Health == 0 then --- if the zombie is dead you get a reward/sword
Tam.Parent = game.Players.LocalPlayer.Backpack
end
end
end
end)
Oh it is inside of a local script. Local scripts can’t access server storage which is where the zombie model is. It needs to be inside of a server script.
this is also a good idea to add to your script, cloning the zombie. When I said server script I meant just a normal script, and yes I would suggest putting it in serverstorage. What client sided means is only the local player can see it and it is not performed on the server. so if you were to change the color of a part client sided only the player that this script executed for will see the part change colors while everyone else will see the color it was before. Local scripts are client-sided while normal scripts are server sided. As you gain more experience scripting you will start to get used to when to use what script and where the best place to put the script is.
I did the script in serverscriptservice.
i get error: ServerScriptService.Script:4: attempt to index nil with ‘Backpack’
my code----
local Killer = game.Workspace.Killer --- its a kill brick
local Trigger = game.Workspace.Trigger --- part that causes zombie to spawn when touched
local Tam = game.ServerStorage.Tam --- its the sword
local Backpack = game.Players.LocalPlayer.Backpack --- backpack of player
Killer.Touched:Connect(function(touchPart) --- checking if the part is touched
local humanoid = touchPart.Parent:FindFirstChild("Humanoid") ---checking if touchPart has a humanoid
local Zomb = game.ServerStorage.Zomb
local Backpack = game.Players.LocalPlayer.Backpack---- its a zombie
if humanoid then --- if humanoid then make the zombie spawn in workspace
local newZomb = Zomb:Clone()
newZomb.Parent = game.Workspace
if humanoid then --- if it has a humanoid its kill it
humanoid.Health = 0
if humanoid.Health == 0 then --- if the zombie is dead you get a reward/sword
Tam.Parent = game.Players.LocalPlayer.Backpack
end
end
end
end)
You are trying to call local player from a server script. The server doesn’t know who the local player is. So what we will do is get the player from the character once the touched event is played. Then access the backpack from the player we now know.
local Killer = game.Workspace.Killer --- its a kill brick
local Trigger = game.Workspace.Trigger --- part that causes zombie to spawn when touched
local Tam = game.ServerStorage.Tam --- its the sword
local Players = game:GetService("Players")
Killer.Touched:Connect(function(touchPart) --- checking if the part is touched
local humanoid = touchPart.Parent:FindFirstChild("Humanoid") ---checking if touchPart has a humanoid
local Zomb = game.ServerStorage.Zomb
if humanoid then --- if humanoid then make the zombie spawn in workspace
local PlayerFromCharacter = Players:GetPlayerFromCharacter(humanoid.Parent)
local newZomb = Zomb:Clone()
newZomb.Parent = game.Workspace
if humanoid then --- if it has a humanoid its kill it
humanoid.Health = 0
if humanoid.Health == 0 then --- if the zombie is dead you get a reward/sword
Tam.Parent = PlayerFromCharacter.Backpack
end
end
end
end)
This should work I may have made a small mistake because I didn’t write this in studio if it doesn’t let me know. Also if you don’t fully understand why this works and yours didn’t before just let me know and maybe I could try to explain it the best I could.
The reason it spawns so many zombies is because it is constantly touching the player which is creating a ton of zombies. You should add a debounce to fix that. After you do this try putting the Players variable inside of the function so it refreshes the player list everytime the function is called.
To prevent multiple zombies from spawning, you would need a cooldown or a debounce. An example of a debounce is this :
local debounce = false
workspace.Part.Touched:Connect(function())
if debounce == false
debounce = true
print("Hello!")
task.wait(1) -- your cooldown amount
debounce = false
end
end)
To implement this into your script, it would look like this :
local Killer = game.Workspace.Killer --- its a kill brick
local Trigger = game.Workspace.Trigger --- part that causes zombie to spawn when touched
local Tam = game.ServerStorage.Tam --- its the sword
local Players = game:GetService("Players")
local debounce = false
Killer.Touched:Connect(function(touchPart) --- checking if the part is touched
local humanoid = touchPart.Parent:FindFirstChild("Humanoid") ---checking if touchPart has a humanoid
local Zomb = game.ServerStorage.Zomb
if humanoid and debounce == false then --- if humanoid and no cooldown then make the zombie spawn in workspace
debounce = true
local PlayerFromCharacter = Players:GetPlayerFromCharacter(humanoid.Parent)
local newZomb = Zomb:Clone()
newZomb.Parent = game.Workspace
if humanoid then --- if it has a humanoid its kill it
humanoid.Health = 0
if humanoid.Health == 0 then --- if the zombie is dead you get a reward/sword
Tam.Parent = PlayerFromCharacter.Backpack
end
end
task.wait(1) -- your cooldown
debounce = false
end
end)