Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I find Character from this?

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

✓ Accepted answer

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)

1 comment

0

Awesome, thank you for the help! I appreciate it, I learned something new today.

This discussion is preserved as it was. New answers, comments, and votes are closed.