How can I check if a player touched a Part?

Hello, I would like to make a local player touch a part and check if its Roles is in a his stringValue (I have already created or not)

how can I do this? I don’t know very well how to create this check

image

In short, as you see in the image it says “Roles” I want to create a check when a player touches a specific “Part” and if its ‘roles name’ is “Good” (for example) then it will print “You are good” of otherwise (else) will show a print “You are not good”.

I share my script, everything is working properly
I only need to obtain the Role Value of the player who touched the Part and do checks.
Example: if his Role == "RoleName" then print something

Any idea how to do it?
Could someone give me a hand, I would appreciate it very much. :frowning:

Script:

local DataStoreService = game:GetService('DataStoreService')
local RanksDataStores = DataStoreService:GetDataStore('RanksDataStores')

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new('Folder', player)
	leaderstats.Name = 'leaderstats' 

	local Ranks = Instance.new('StringValue', leaderstats)
	Ranks.Name = 'Roles'
	
	local RanksData

	local success, errormessage = pcall(function()
		RanksData = RanksDataStores:GetAsync('Ranks_'..player.UserId)
	end)
	
	if RanksData and success then
		player.leaderstats.Roles.Value = RanksData
		print("Data has been saved!")
	else
		warn(errormessage)
		print("There is something wrong.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Ranks = player.leaderstats.Roles
	local success, errormessage = pcall(function()
		RanksDataStores:SetAsync("Ranks_" .. player.UserId, Ranks.Value)
	end)
end)

You could use a .Touched event which checks whether the player has a certain role.

But I need touch a part with the mouse, how can i do it? :frowning:

Use a ClickDetector | Roblox Creator Documentation.

1 Like

and how can I get the value of the string (Roles string value) parented to the player in the localscript? :scream:

Use the .MouseClick event, as it uses a parameter containing the player who clicked.

Example:

ClickDetector.MouseClick:Connect(function(player)
local value = player.StringValue.Value -- change this to where your value is

-- do stuff

end)
1 Like

I’m assuming you’re using leaderstats, so you could take the player and check if the role in their leaderstats is correct.

Just do this

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        player["YourThing"].Value = "blahblahblah"
    end
end)

By the way in the localscript only one player will see it lol

You need to do it in a server script if you want the whole server too see it

Cool! this worked! Thank you so much everyone! :smiley: :smiley:

1 Like