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

Why is my sliding black rectangle not triggering the function when it reaches the center?

Asked by
nap516 87
5 years ago

I currently have a white rectangle that contains a red rectangle in the middle, basically there is a black rectangle at the start that keeps moving to it's right. The player must press B when the black is in the center.

Currently there are no errors showing up in output but there is something wrong, i'm using a print() function to test it and nothing prints.

Also, I was wondering if there was a property/easier way to detect contact from one text label to another to clear up my cluttered if-then statement.

I'm out of ideas and I don't know what to do. Here's the code:

repeat wait() until game.Players.LocalPlayer --Waits for the Player
local player = script.Parent.Parent --Player
local mouse = player:GetMouse() --Get the player's mouse.

mouse.KeyDown:connect(function(key) --Function
    if key == "b" then --Waits for the key to be pressed, once it is, the statement if fired.
    player.PlayerGui.ReversalGui.Frame.Bar.LocalScript1.Disabled = true
    wait(0.25)
    if player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.2, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.24, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.28, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.32, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.36, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.4, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.44, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.48, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.52, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.56, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.6, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.64, 0, 0, 0) or player.PlayerGui.ReversalGui.Frame.Bar.Position == UDim2.new(0.68, 0, 0, 0) then --This is what I was referring to earlier
    print("work") --Printing check to make sure it works.
end
end
end)

I'd really appreciate some help. I've looked at the Wiki page for UDim2 already so I honestly don't know what to do.

-Nap516

0
The LocalPlayer will always exist Ziffixture 6913 — 5y
0
Use UserInputService (InputBegan, InputEnded) to detect keystrokes instead of KeyDown. It's not printing because one of the if statements are not going through. Suggest to add another test print inside the first if statement to double check if that ones working/not. hellmatic 1523 — 5y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

KeyDown, as I recently mentioned in another Question, is deprecated of mouse, meaning it would never run. It’s best to resort to UserInputService‘s:IsKeyDown() for holding key recognition. it would look like so when applied

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(InputObect)
    if (UserInputService:IsKeyDown(Enum.KeyCode.B)) then
        print("User is Holding the B key down")
    end
end)

Hope this works for you. If so don’t forget to accept and upvote!

0
tysm! nap516 87 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Well, even though Feahren beat me to this, I'll still help you out a bit, as far as the if-then statements, since you can't actually detect if TextLabels are colliding.

If you place the LocalScript (with this code) into the StarterGui, you can effectively shorten the amount of clauses you have written out like so:

local player = game.Players.LocalPlayer -- Player

local bar =  script.Parent.ReversalGui.Frame.Bar

game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.B then
        bar.LocalScript1.Disabled = true
        wait(0.25)
        local check = bar.Position.X.Scale
        if check >= 0.2 and check <= 6.8 then
            print("Press Now!")
        end
    end
end)

Again, KeyDown is deprecated, use UIS (UserInputService)

0
Thank you both so much nap516 87 — 5y

Answer this question