How do you make sure both variables are the same but also are in the right place - lego-mindstorms

I am making a simple game in LEGO EV3 programmer (default software) and I have a code that I don't know how I'd make.
I have 2 variables -
if player is in the right spot,
if bullet is in the right spot.
I want to make it so it is compared, and if the variables both equal, and are in the same position, it makes another variable true.
Therefore...
if ((IF PLAYER & IF OBJECT) = SAME) & ((IF PLAYER = TRUE) & (IF OBJECT = TRUE)) then VARIABLE = TRUE;
Or something like that...

Your code should not have "if if" anywhere, that just looks wrong.
What do you mean by "if player = true"? This doesn't make sense when you read it. A player cannot be true.
The code should look like if player = bullet then shot = true. This assumes that the player and the bullet variables represent the location of these objects. (I know nothing about Mindstorms, by the way.) Maybe you need to replace the first = with ==, depending on how the programming language needs it.

What language that you use?
I am used python to wrote code in my brick before.
So I may code as this:
spotA = player.spot()
spotB = object.spot()
anotherVer = false
if spotA == spotB:
anotherVer = true
And also, I think in all languages, that relational expression is use like
==
!=
||
&&
etc.
Hope this will help you. :)

Related

Roblox - detect if mesh is hit by hand

I am not sure where to start so asking for some help. I want to create a script that detect if a certain mesh is hit by players right hand. Every time the mesh is hit I want to increase points for player by 1.
Anyone who can nudge me in the right direction here?
Thanks!
EDIT:
I have added this script in StarterCharacterScripts:
game.Players.LocalPlayer.Character:WaitForChild("RightHand").Touched:Connect(function(hit)
local part1 = workspace.CoinsClouds["Meshes/SackOfGoldNoCoins1"]
part1.Touched:Connect(function(hit)
if hit.Name == "RightHand" then
print(hit.Name)
end
end)
end)
This will register when I bump into the part with the right hand, BUT it will register it 5-20 times in a split second every time I bump into the part with the right hand. See attached image. Anyone know why? I would like it to register only once when the right hand is bumped against the part, or even better, only when the user punch the part/mesh. I have tried to add a wait after RightHand is found, but that doesn't work.
PS! I don't know if this is the right way to script it...
Below is an example of the code you would use. It will check if your part is touched by either the right or left hand and it will run whatever is inside of your if statement, in this case it will increase whatever your score is by 1.
local part = workspace.Part -- reference the part here
local debounce = false --used to check for debounce
part.Touched:Connect(function(hit)
if hit.Name == "RightHand" or "LeftHand" then -- checks if a hand touched it
if not debounce then --Check that debounce variable is not true
debounce = true --currently touching the part
print("hit")
wait(1)
debounce = false
end
end
end)
In relation to your new question it is to do with debounce. Roblox has an entire article on it here: https://developer.roblox.com/en-us/articles/Debounce
Basically you would have to add a new if statement to check that the player isn't already touching this part if it is it wont repeat the what is inside the if statement. The code sample above has been edited to work with debounce.

Can i use an if statement for a player name? [ROBLOX]

I am on Roblox again, and I wanna make a script where when a part is touched, if the player name is lorenzoomh, set the brickcolor to green. But if not, set it to red. my current code:
script.Parent.Touched:Connect(function(igottouched)
if game.Players.LocalPlayer.Name == "lorenzoomh" then
script.Parent.BrickColor = BrickColor.Green()
else
script.Parent.BrickColor = BrickColor.Red()
end
end)
which doesnt work btw
The answer to your question, "can I use a player's name in an if-statement" is yes. Your problem is that the LocalPlayer doesn't exist where you're trying to use it from.
See the docs for Players.LocalPlayer :
This property is only defined for LocalScripts ... as they run on the client. For the server (on which Script objects run their code), this property is nil.
You need to access the Player object a different way. The Touched event on a Part gives you access to the other object that touched it. You can use that to check if the other part belongs to a player's character model.
local target = game.Workspace.Part
-- listen for a player touching a block
target.Touched:Connect( function(otherPart)
-- check that the thing that touched is a character model
local character = otherPart.Parent
local isCharacter = character:FindFirstChild("Humanoid") ~= null
if isCharacter then
-- the character model name is usually the name of the player so use that
if character.Name == "lorenzoomh" then
target.BrickColor = BrickColor.Green()
else
target.BrickColor = BrickColor.Red()
end
end
end)
The Players service has a method for correlating a character model with a Player object specifically if a simple name check is not sufficient for your use case.

Roblox - creating a multistory maze

I am trying to create a multistory maze. I found it fairly easy to create the first level, then I realized I had no idea how to simply raise this first level up in order to create a second level beneath it.
Also, is there a way to 'fuse' all of these wall parts into one object and then raise this object up?
Edit: Much to my embarrassment, their is a way to fuse objects. The 'Union' tool is what I needed, but had no idea existed. I 'fused' (unioned) the various parts that made up my walls and joined them together into one big part. After that unioning, moving the entire maze upwards became quite easy.
I don't understand your problem but I think that you're making a 3D maze in roblox and you want the first level to go up and the second level to form below the level.
If the maze is NOT procedurally generated AND the maps are built by hand. Then you can make the script detect if the player won, and then raise the first level by either using tween or using loops (I'd recommend tween because loops and linear tweening does the same), and then make an effect that shows it forming (Transparency, parts coming back together, etc..).
I will show you the simplest example. But you can add whatever you want
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(0.5, Enum.TweenStyle.Linear, Enum.TweenDirection.Out) --Customize it to your liking
local levels = game.LevelStorageParent.LevelFolderOrModelHere:GetChildren()
local pos = workspace.Level1.Position --Change (Not the levels because we are not cloning this)
local levelYRaise = 10 --Put any number or just get bounding box for full raise
ts:Create(workspace.Level1, ti, {Position = Vector3.new(pos.X, pos.Y+levelYRaise, pos.Z):Play()
local newLevel = levels.Level2:Clone()
newLevel.Parent = workspace
newLevel.Pos = workspace.Level1.Position - Vector3.new(0, workspace.Level1.Size.Y, 0)
newLevel.Transparency = 1
ts:Create(newLevel, ti, {Transparency = 0}):Play()
Change the code to your liking and your hierarchy names and parenting

Roblox - while testing trusses work when i drag them in, but when i use script to set all the stuff, then i cant climb it

I am using a script to set cancollide = true and transparency = 0, but i cant climb the truss. but when I'm already in testing mode (in studio) and i drag in the same truss, i can climb it, and i am looking at properties, its same, both anchored is true, they are touching same parts, i dont know why this is happening. Please help thanks :)by the way, i am making tycoon and this is script i am using:
wait(1)
amount = 0 -- cost of model
owner = script.Parent.Parent.Owner
local stun = false
pcall(script.Parent.Head.Touched:connect(function(hit)
if hit.Parent ~= nil then
player = game.Players:findFirstChild(hit.Parent.Name)
if not stun and player ~= nil then
if player.Name == owner.Value then
if player:findFirstChild("leaderstats") ~= nil then
stats = player:findFirstChild("leaderstats")
if stats.Money.Value >= amount then
stun = true
stats.Money.Value = stats.Money.Value - amount
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
wait(1)
stun = false
end
end
end
end
end
end))
dont worry about other stuff, it works, its just this part that matters now:
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
Please help :( this is the problem with the ladder using script not working and same dragged in from toolbox truss working, iv done this with many trusses and ladders and same result :(
So, a error I noticed in your code, is that you erase your Character's Head, which would kill your character, or possibly throw a error if the .Touched event fires two times.
There are many efficency errors in this script, such as .Transparency = (0), or doing script.Parent.ladder instead of using variables, but it really is no problem most of the times. Something you could try, is to use Instance.new() to create the ladder.If this game is FilteringEnabled on (Expermiental mode off), please note your script won't work as intended if it is a LocalScript, it will only work well on Server-Side (aka, a normal Script instead of a LocalScript)

Odd EXC_BAD_ACCESS when calling contactTestBetweenBody

I have a 3D scene created in scene kit comprising an area surrounded by invisible walls and I want to lob physics objects into this area in such a way that they can't escape once they're in. I had a mind to achieve this in the following fashion:
Create a wall object
Create a 'solidifier' that fits neatly inside the walls
Set each object's isInScene variable to false
Lob them in the vague direction of the solidifier
Upon each update, if an object is touching the solidifier but is not touching the walls, I change its collision mask to include the walls and set isInScene to true so I don't have to check it again.
This often seems to work very well, but every so often (and sadly quite often) I get an EXC_BAD_ACCESS error out of nowhere. The offending method seems to be contactTestBetweenBody, which I am using to determine when an object is touching either the walls or solidifier at times when normal collision detection is turned off. This is necessary to prevent the objects simply bouncing off the outside of the wall object.
Here's a small snippet of code to illustrate. Incidentally, 'objects' is a structure that retains a reference to the node along with other useful details:
if let solid = solidifier?.physicsBody, let wall = walls?.physicsBody {
let world = scene.physicsWorld
for i in 0 ..< objects.count {
if objects[i].isInScene == false, let body = objects[i].node.physicsBody {
let contactSolidifier = world.contactTestBetweenBody(body, andBody: solid, options: nil)
if contactSolidifier != nil {
let contactWall = world.contactTestBetweenBody(body, andBody: wall, options: nil)
if contactWall == nil {
objects[i].isInScene = true
body.collisionBitMask = CollisionMask.allSolids.rawValue
}
}
}
}
}
I found a much, much better solution to the problem that I just plain didn't think of for some reason. Forget about this ridiculously convoluted means of keeping the objects in view. Instead, just make the area you want to retain the objects within and reverse its facet direction and normals.
What I didn't realise was that SceneKit uses backface culling on collision detection too, so if a physics object hits the OUTSIDE of an object that is inside out it will pass clean through.
I would still be interested to know the reason for the bad access error still though, as the contact test methods are useful and I may want to use them for other reasons in the future.