so basically im trying to make a part where if you touch it your head explodes. it works in studio solo test, but when i join in game, it says" Workspace.die.Script:6: attempt to index field 'LocalPlayer' (a nil value)" it's a regular script, not a localscript, if that helps.
keep in mind im a beginner beginner
local die = script.Parent debounce = false script.Parent.Touched:Connect(function(hit) local p = game.Players.LocalPlayer.Character if hit.Parent:FindFirstChild("Head") and debounce == false then debounce = true local b = Instance.new("Explosion") b.BlastPressure = 1 b.BlastRadius = 1 b.Position = p.Head.Position b.Parent = game.Workspace p.Head:Remove() debounce = false print("worked") end end)
The problem is, you can't use "LocalPlayer" inside a server script. This is because what is done on a server script is done through the server, and what's done on a local script is done through the client (hence the name "local"). To fix this, don't use local player.
You need to keep everything how it is, but make it use a different way of finding the player. What you should be using to make this work is something called GetPlayerFromCharacter. There is an example located on the wiki, so I'm not really gonna explain what it does. I will fix your script in this answer, but please read over the wiki link I posted to understand what I did.
local die = script.Parent local debounce = false script.Parent.Touched:Connect(function(hit) local p = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) --using GetPlayerFromCharacter if p.Character:FindFirstChild("Head") and not debounce then --same as debounce == false debounce = true local b = Instance.new("Explosion") b.BlastPressure = 1 b.BlastRadius = 1 b.Position = p.Character:FindFirstChild("Head").Position b.Parent = workspace --I prefer using workspace rather than game.Workspace p.Head:Destroy() --don't use Remove(), use Destroy() wait(2) --add a wait so it doesn't break the script debounce = false end end)