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

How do I find Character from this?

Asked by
ElBamino 153
2 years ago

How would I find the character from this code below? I know how to call the character from other things but, I'm trying something different this time. I couldn't seem to find much about this myself online and, I would love if you could provide me with some links possibly? I appreciate the help! Thank you in advance as always.

local debounce = true
script.Parent.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    --Call character but, I don't how to from this.
    if player and player.leaderstats.Tix.Value >= 9000  and debounce == true then
        --Caramelldansen insert or whatever
        debounce = false
        wait(1)
        debounce = true
    end
end)

1 answer

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

Touched event returns for the touched part. They does not return the character name, but the body part touched. So to locate the Character in a Touched event, do local character = hit.Parent.

Also you did a little mistake. Since sometimes some other parts like Baseplate will also touch the part, which also fires the event and returns an error "Workspace is not a Player" or something like that. You can detect if it have a humanoid, if not, return the function.

Lastly, your debounce script also have mistake. You can still bypass the debounce by making it true and false. What are you going to do is make a if-statement, if the debounce is true, then make it pass. Else don't!

I've set up the script for you. If you have any problems, comment below so I will get the notification

Script:

local debounce = true

script.Parent.Touched:Connect(function(hit)
    if debounce == true and hit.Parent:FindFirstChild("Humanoid") then
        debounce = false
        local character = hit.Parent
        local player = game:GetService("Players"):GetPlayerFromCharacter(character)
        if player and player.leaderstats.Tix.Value >= 9000 then
            print(player.Name .." Tix value is 9000 or more than 9000!")
            -- Do stuff here
        end
        wait(1)
        debounce = true
    end
end)
0
Awesome, thank you for the help! I appreciate it, I learned something new today. ElBamino 153 — 2y
Ad

Answer this question