This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
fergregerg's profile picture

Player detector

Started by fergregerg, 17 May 2016 - 05:13 AM
fergregerg #1
Posted 17 May 2016 - 07:13 AM
Im still a big noob to this stuff and I need help with a program to detect other players. Any help is great. Please and thank you! Sorry my codes a mess.





What im trying to do

Detect if a player is me or my friend or no one and keep redflag set to false.
Set redflag to true when anyone else enters the area and put off a redstone signal to the left.
redstone torch up front stops program


What it does do

Detects all players and sets redflag to true then gives off a redstone signal to the left

a redstone signal input up front will stop the program


os.loadAPI("ocs/apis/sensor")
local tzs = sensor.wrap("top")
local redflag = false
local power = false
--print(textutils.serialize(tzs.gettargets())
while 1 > 0 do
--front killer
if power == true then
redstone.setAnalogOutput("left",0)
return
end
if redstone.getAnalogInput("front") == 15 then
power = true
open = false
end
-- front killer end
for k,v in pairs(tzs.getTargets()) do
  if v ~= nil and v["IsPlayer"] == true then
	redflag = true
  else
	redflag = false
end end
	os.sleep(0.5)
  
	  
if redflag == true then
	 redstone.setAnalogOutput("left",15)
	 else
	 redstone.setAnalogOutput("left",0)
	 end
end
Edited on 17 May 2016 - 05:15 AM
Bomb Bloke #2
Posted 17 May 2016 - 09:03 AM
rs.setOutput() and rs.getInput() are a bit simpler than rs.setAnalogOutput() and rs.getAnalogInput(), as the former two functions assume full/any strength, whereas the latter two require specific strength settings.

You don't need to check if "power is true" is true. You can straight up check if power is true:

if power then

Likewise, any value that isn't false or nil counts as true for the purposes of a conditional check:

if v and v["IsPlayer"] then

… though even then, checking v isn't necessary, because the pairs loop will never set it to nil.

Currently, your sensor seems to be setting redflag to true if the last entry in tzs.getTargets() is a player, and false if it isn't (the other entries in the table don't matter, because you overwrite redflag on every iteration of your "for" loop - so only the last iteration counts).

You'll need to figure out how to get player names from the sensor output. To my loose understanding, "v" should represent these names.

Let's say we define a table with the names of you and your friends as its keys:

local friends = {["fergregerg"] = true, ["Tom"] = true, ["Dick"] = true, ["Harry"] = true}

Now we can re-write the "for" loop:

local redflag = false

for player, details in pairs(tzs.gettargets()) do
  if details.IsPlayer and not friends[player] then
    redflag = true
    break
  end
end

rs.setOutput("left", redflag)

This way, redflag's always false unless an unknown player's in the area.

If you're still having trouble, it'd be helpful to know exactly what the content of tzs.gettargets() is. I suspect you're using incorrect capitalisation on "IsPlayer", for one thing.
Edited on 17 May 2016 - 07:08 AM
fergregerg #3
Posted 17 May 2016 - 04:04 PM
It doesnt work, it still puts off a signal for anyone including me. How can I print the data i am having trouble with that? I don't get the gettargets function that much either
HDeffo #4
Posted 17 May 2016 - 05:16 PM
Just as a warning if I remember correctly there's a bug in ocs where occasionally the functions return nil instead of a table usually if someone is just out of its range by half a block or so. As a precaution make sure everything ocs sends you isn't nil before sending it to a function like pairs
Bomb Bloke #5
Posted 18 May 2016 - 01:41 AM
If you're still having trouble, it'd be helpful to know exactly what the content of tzs.gettargets() is. I suspect you're using incorrect capitalisation on "IsPlayer", for one thing.