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

How do I identify players position as a value? Thanks!

Asked by 6 years ago

In my single player game, I want to make a grass field for the player to walk in, but since the field is so big, I want to make a script that will load in only grass that is near you. What I want to happen, is that whenever a player gets close enough (230 studs) to an invisible part in the model 'GrassPatchSpawnPoints', the script will get a model called 'GrassPatch' from ScriptStorage and place it at the position of that invisible part. Whenever I load it in, output is telling me that line 3 is an attempt to index a nil value. I need to figure out how to identify to position of the player as a value, so then the script can compare the number of the distance of the character to a part. What should I do?? I'm a beginner so if it looks stupid, please don't be harsh.

distance = 230 --num value to studs "put radius in stud metric"

avatar = game.Players:FindFirstChildOfClass("Player").Position
GrassSpawn = workspace.GrassPatchSpawnPoints:GetChildren()

for g=1, #GrassSpawn do
    gspawn = GrassSpawn[g]
end

A = avatar
B = gspawn.Position

if (A - B).magnitude <= distance then
    gClone = game.ServerStorage.GrassPatchClone:Clone()
    gClone.parent = workspace
    gClone.Position = B
    gClone.Rotation = Vector3.new(0,math.random(0,175),0)

else
    gspawn.transparency = 1
end
0
The physics engine still has to handle parts, even if they are transparent. I suggest destroying them and placing them back in. hiimgoodpack 2009 — 6y
0
Position is not a property of player, rather player.Character["Head"] creeperhunter76 554 — 6y
0
it still doesn't work. currently at line 3 it says `avatar = game.Players:FindFirstChildOfClass("Player").Character["Head"].Position` BronzedMocha 60 — 6y

2 answers

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

alrighty let's start from scratch. :p

First let's set up a LocalScript and place that in StarterPack. Then let's set our variables up

local plr = game.Players.LocalPlayer
local grass = game.ReplicatedStorage.GrassModel --change this to the grass model, place it in replicatedstorage
local closeto = workspace.Part --this is the part that we will check if the player is near or not

Next let's settup our functions

local plr = game.Players.LocalPlayer
local grass = game.ReplicatedStorage.GrassModel
local closeto = workspace.Part

plr.CharacterAdded:connect(function(char)
    local grassclone=nil --setup var
    char:WaitForChild("Humanoid").Died:connect(function()
        script.Disabled=true
        if grassclone then
            grassclone:Destroy()
        end
    end)
end)

Next we will create our loop to check if the player is near closeto

local plr = game.Players.LocalPlayer
local grass = game.ReplicatedStorage.GrassModel
local closeto = workspace.Part

plr.CharacterAdded:connect(function(char)
    local grassclone=nil --setup var
    char:WaitForChild("Humanoid").Died:connect(function()--check if your dead
        if grassclone then
            grassclone:Destroy()
        end
        script.Disabled=true
    end)
    char:WaitForChild("HumanoidRootPart")
    while true do wait(1)
        if (char.HumanoidRootPart.Position-closeto.Position).Magnitude<230 then
            grassclone=grass:Clone()
            grassclone.Parent=workspace
        else
            if grassclone then
                grassclone:Destroy grassclone=nil
            end
        end
    end
end)

Make sure you use filteringenabled for this so that your doing this on a per player basis.

your problems (fixing what i saw here will not mean it will automatically work)

distance = 230

--your not using Local
local avatar = game.Players:FindFirstChildOfClass("Player").Position --no position property of player
local GrassSpawn = workspace.GrassPatchSpawnPoints:GetChildren()

local gspawn

for g=1, #GrassSpawn do --this loop isn't really helping you for anything, your setting the variable gspawn over and over right here and when you get past this it will only be the last on you got through
    gspawn = GrassSpawn[g]
end --also you should be doing this loop like this
--ex
for _,v in pairs(GrassSpawn) do --just GrassSpawn because you gave that variable a table (GetChildren())

end

local A = avatar --because you still haven't giving this variable a proper position it should be like this
A = avatar.Character.HumanoidRootPart.Position
local B = gspawn.Position --will only give you the one position

if (A - B).magnitude <= distance then
    gClone = game.ServerStorage.GrassPatchClone:Clone()
    gClone.parent = workspace
    gClone.Position = B
    gClone.Rotation = Vector3.new(0,math.random(0,175),0)

else
    gspawn.transparency = 1 --your not deleted gspawn so what's the point of making this script if your not saving space
end
0
I'm not asking for a new script. As nice as it is that you are giving me a script, the only way i want to learn and create is by making scripts myself. The last thing I want is a script I don't understand, because if I ever wanna change a detail, I wont be able to understand it. I don't care how unhelpful my script above is, I want to fix my problems step by step. BronzedMocha 60 — 6y
0
@ChuckMan02 alright, so here's what's wrong with your script first off on line 3 you your setting avatar but your trying to get Position of the player, there is no position property of player so you should remove that. to get the position of the character you should navigate from the player to the character then get the position of the player's humanoidrootpart like so, (1) DanzLua 2879 — 6y
0
@ChuckMan02 avatar.Character.HumanoidRootPart.Position , you should use this as your A variable so, A = avatar.Character.HumanoidRootPart.Position next, your doing this thing with your gspawn, first you should be using lcoal variables, search for that in the wiki, so like but this isn't even going to work, here i'll just edit my answer lol DanzLua 2879 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

As creeperhunter76 said, position is not a value of the player, rather a part of the player. Thats what nil value means, it doesnt exsits, it is Nil

BTW you might wanna rethink how you are doing this, as grass other players are near will load for all the other players, defeating the purpose. (unless you want this).

look into local parts

0
This is a single player game however. Does using local scripts matter when the max players will be only 1? And is there a way to convert Vector3 from the position of the player to a value? BronzedMocha 60 — 6y
0
Oh, then nvm then. Using server scripts in a 1 person game is the best. However, for GUIS, use local scripts ALWAYS. and for the vector 3 to valaue thing, the vector 3 is already a value. Please elaborate on that User#17125 0 — 6y

Answer this question