Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

why doesn't my ontouch part script work in game when it works in roblox studio's solo test?

Asked by 7 years ago
Edited 7 years ago

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) 
0
fixed the couldnt remove head AngelGd 0 — 7y
0
removed debounce to make the script work more than once AngelGd 0 — 7y
0
I replied on a different account under the name "MrSmenry" on my answer. Hopefully that explains why it was erroring. mrsbigballz 8 — 7y
0
Do you have FE on? If so, test your game through the 'start' button which has an icon that looks like a server. Viking359 161 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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) 
0
Finally, someone who actually knows about that you cannot use local player from a server script. But they might've stolen it from my bio. hiimgoodpack 2009 — 7y
0
i knew that, but i was just making sure, also to find how i could fix it. also i saw ur bio just a few minutes ago from a recent answer AngelGd 0 — 7y
0
two questions: why use destroy instead of remove, and how can i make the script work more than once. since i did it once, it no longer worked when i did it the second time. also it said "Head is not a valid member of Player" so it couldnt remove the head AngelGd 0 — 7y
0
The reason you use destroy over remove is because remove is deprecated. I think the reason it breaks is because there is a denounce, but you didn't add a wait so everything happens instantly. This causes that error because you're removing their head so they don't have it anymore. R_alatch 394 — 7y
View all comments (2 more)
0
okay thanks AngelGd 0 — 7y
0
but i still dont really understand what you did with getplayerfromcharacter AngelGd 0 — 7y
Ad

Answer this question