generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: CharacterAdded never executing?

Previous Thread :: Next Thread 
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 02:37 PM
My following script doesn't run anything past the 4 initial prints, when it should be printing lots of other things. I can't seem to find the problem anywhere

math.randomseed(tick()%16)
local grass = {}
local cave = {}
local beach = {}
local water = {}
wait(.01)
for i,v in pairs(_G.Pokemon) do
for i=1,v.SpawnRate do
if v.Biome == "Grass" then
table.insert(grass, v.Name)
elseif v.Biome == "Cave" then
table.insert(cave, v.Name)
elseif v.Biome == "Beach" then
table.insert(beach, v.Name)
elseif v.Biome == "Water" then
table.insert(water, v.Name)
end
end
end
print("Grass: "..#grass)
print("Cave: "..#cave)
print("Beach: "..#beach)
print("Water: "..#water)
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded(function(chr)
print(chr.Name)
spawn(function()
local running = false
chr.Humanoid.Running:connect(function(s)
if s>0 then
running = true
else
running = false
end
end)
local last_Pokemon = tick()
while wait(.1) do
print(running)
local leg = chr:FindFirstChild("Left Leg")
local ray = Ray.new(leg.Position, Vector3.new(0,-10,0))
local hit, position = workspace:FindPartOnRay(ray, chr)
if hit ~= nil and running then
local color = tostring(hit.BrickColor)
local colors = {
["Shamrock"] = "Grass",
["Pine Cone"] = "Cave",
["Bright bluish green"] = "Water",
["Cashmere"] = "Beach"
}
local Type = colors[color]
if Type ~= nil then
print(Type)
local chance = 100
local rand = math.random(1,chance)
if rand == math.ceil(chance/2) and (tick() - last_Pokemon >= 20) then
print("Pokemon?")
last_Pokemon = tick()
local spawns
if Type == "Water" then
spawns = water
elseif Type == "Grass" then
spawns = grass
elseif Type == "Cave" then
spawns = cave
elseif Type == "Beach" then
spawns = beach
end
local Rand = spawns[math.random(1, #spawns)]
print(#spawns)
if Rand ~= nil then
print(Rand)
else
print("No pokemon in this biome :( "..Type)
end
end
else
print(color)
end
end
end
end)
end)
end)


-iJava
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
09 Aug 2016 02:42 PM
plr.CharacterAdded(function(chr)
should be
plr.CharacterAdded:connect(function(chr)


Report Abuse
killerbot29003 is online. killerbot29003
Joined: 04 Oct 2014
Total Posts: 3054
09 Aug 2016 02:42 PM
try it live



This siggy is copyrighted ©
Report Abuse
EncodedLua is not online. EncodedLua
Joined: 02 May 2012
Total Posts: 1555
09 Aug 2016 02:42 PM
I would recommend putting the PlayerAdded handler at the top. That way it's sure to run. If you try running the test server with 1 player, wait and then run it with a second, the second one should work. If that happens, then this is your problem.
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
09 Aug 2016 02:43 PM
PlayerAdded is fine.


Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 02:44 PM
I just pasted it wrong, I'm using that @Soybeen


-iJava
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
09 Aug 2016 02:44 PM
Where is this code running?


Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 02:47 PM
workspace, under a ServerScript


-iJava
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
09 Aug 2016 02:54 PM
spawn(f) executes function f after thread next yields. I think you should try a coroutine instead(?)

Not sure if this is the problem.




Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 02:54 PM
I switched so spawn from coroutine thinking that was the issue, but it changed nothing. The print above the coroutine never prints either


-iJava
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
09 Aug 2016 02:58 PM
But playeradded prints?


Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:07 PM
~~Magic~~
????????



math.randomseed(tick())--Shortening the number will only make it not as random, leave it at tick().
grass = {}
cave = {}
beach = {}
water = {}
types = {{'Grass', grass},{'Cave', cave},{'Beach', beach},{'Water', water}}
chance = 100
colors = {
['Shamrock'] = 'Grass',
['Pine Cone'] = 'Cave',
['Bright bluish green'] = 'Water',
['Cashmere'] = 'Beach'
}

function sort(input)
for i,v in pairs(types) do
if v[1] == input then
return v[2]
end
end
end

for i,v in pairs(_G.Pokemon) do
for i=1,v.SpawnRate do
table.insert(sort(v.Biome), v.Biome)
end
end

print('Grass: ', #grass, ' Cave: ', #cave, ' Beach: ', #beach, ' Water: ', #water)

game.Players.PlayerAdded:connect(function(p)
p.CharacterAdded(function(c)
local h = c:WaitForChild'Humanoid'
local leg = c:WaitForChild'HumanoidRootPart'
local last_Pokemon = tick()
local running = false
local lv = 0

function CheckForPokemon(cv)
while wait(0.1) do
if cv < lv or not running then
break
end
local ray = Ray.new(leg.Position, Vector3.new(0,-10,0))
local hit, position = workspace:FindPartOnRay(ray, c)
if hit and running then
local color = tostring(hit.BrickColor)
local Type = colors[color]
if Type then
local rand = math.random(0, chance)
if rand == math.ceil(chance/2) and (tick() - last_Pokemon >= 20) then
last_Pokemon = tick()
local spawns = sort(Type)
local Rand = spawns[math.random(1, #spawns)]
if Rand then
print(Rand)
end
end
end
end
end
end

h.Running:connect(function(s)
if s > 0 then
running = true
lv = lv + 1
CheckForPokemon(lv)
else
running = false
end
end)

end)
end)
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 03:09 PM
@Dev
Thanks, looks a lot cleaner, but I get this error now:

15:09:11.312 - Workspace.WildPokemon:23: bad argument #1 to 'pairs' (table expected, got nil)
15:09:11.313 - Stack Begin
15:09:11.313 - Script 'Workspace.WildPokemon', Line 23



-iJava
Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:10 PM
Line 32 to:
p.CharacterAdded:connect(function(c)
Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:11 PM
Line 23 is your problem it's your hidden array.
aka: _G.Pokemon
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 03:12 PM
I see, thanks. Adding a wait above that should fix it (I define _G.Pokemon in a different script, so there's a slight delay)


-iJava
Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:13 PM
Also made it so it isn't loop all the time because that's just dumb, so now the server won't need as much resources for each player. :)
Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:14 PM
Why are you even using global functions? Module scripts>
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 03:15 PM
Thanks!
However, for some reason it still isn't working. It prints the first print as it did in the original, but adding this after CharacterAdded and there's nothing printed:
print("Test")

could it just be that I'm in studio? My script was working fine with CharacterAdded in studio an hour ago, and my other scripts using it work fine as well


-iJava
Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:16 PM
Pokemon = require(script:WaitForChild'MassivePokemonArray')--Module scripts are better to use then global functions.
Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:16 PM
Did you change line 32?
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 03:19 PM
Line 32 is p.CharacterAdded for me

there's no errors anymore, I added a wait(.01) under math.randomseed


-iJava
Report Abuse
DevVince is not online. DevVince
Joined: 08 Nov 2008
Total Posts: 9245
09 Aug 2016 03:21 PM
You should really switch over to module scripts instead of global functions.

Anyways this should be line 32:
p.CharacterAdded:connect(function(c)
Not:
p.CharacterAdded
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 03:22 PM
Oh, yeah I fixed that part already

I might switch to a module script later, I've just never used one before


-iJava
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
09 Aug 2016 03:28 PM
Still, nothing is printing
This is what I'm using currently:

math.randomseed(tick())--Shortening the number will only make it not as random, leave it at tick().
grass = {}
cave = {}
beach = {}
water = {}
types = {{'Grass', grass},{'Cave', cave},{'Beach', beach},{'Water', water}}
chance = 10
colors = {
['Shamrock'] = 'Grass',
['Pine Cone'] = 'Cave',
['Bright bluish green'] = 'Water',
['Cashmere'] = 'Beach'
}

function sort(input)
for i,v in pairs(types) do
if v[1] == input then
return v[2]
end
end
end
wait(.01)
for i,v in pairs(_G.Pokemon) do
for i=1,v.SpawnRate do
table.insert(sort(v.Biome), v.Biome)
end
end

print('Grass: ', #grass, ' Cave: ', #cave, ' Beach: ', #beach, ' Water: ', #water)

game.Players.PlayerAdded:connect(function(p)
p.CharacterAdded:connect(function(c)
print("Test")
local h = c:WaitForChild('Humanoid')
local leg = c:WaitForChild('HumanoidRootPart')
local last_Pokemon = tick()
local running = false
local lv = 0

function CheckForPokemon(cv)
while wait(0.1) do
if cv < lv or not running then
break
end
local ray = Ray.new(leg.Position, Vector3.new(0,-10,0))
local hit, position = workspace:FindPartOnRay(ray, c)
if hit and running then
local color = tostring(hit.BrickColor)
local Type = colors[color]
if Type then
local rand = math.random(0, chance)
if rand == math.ceil(chance/2) and (tick() - last_Pokemon >= 2) then
last_Pokemon = tick()
local spawns = sort(Type)
local Rand = spawns[math.random(1, #spawns)]
if Rand then
print(Rand)
else
print("Nope")
end
end
end
end
end
end

h.Running:connect(function(s)
if s > 0 then
running = true
lv = lv + 1
CheckForPokemon(lv)
else
running = false
end
end)

end)
end)


-iJava
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image