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
3 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.

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

1 answer

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 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:

01local debounce = true
02 
03script.Parent.Touched:Connect(function(hit)
04    if debounce == true and hit.Parent:FindFirstChild("Humanoid") then
05        debounce = false
06        local character = hit.Parent
07        local player = game:GetService("Players"):GetPlayerFromCharacter(character)
08        if player and player.leaderstats.Tix.Value >= 9000 then
09            print(player.Name .." Tix value is 9000 or more than 9000!")
10            -- Do stuff here
11        end
12        wait(1)
13        debounce = true
14    end
15end)
0
Awesome, thank you for the help! I appreciate it, I learned something new today. ElBamino 153 — 3y
Ad

Answer this question