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
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
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