How do i edit this script so when player touches part it launches code

local Workspace = game.Workspace

game.Players.PlayerAdded:connect(function(Player)
	Player.CharacterAdded:connect(function(Character)
		if Player.userId > 0 then
			if game:GetService("MarketplaceService"):PlayerOwnsAsset(Player,PremiumID) == true then
				NewVip = Instance.new("IntValue")
				NewVip.Name = Player.Name
				NewVip.Parent = Workspace.Settings.PlayerPremium
			end
		end
		if Workspace.Settings.PlayersPlaying:FindFirstChild(Player.Name) == nil then
			local New = Instance.new("BoolValue")
			New.Name = Player.Name
			New.Value = false
			New.Parent = Workspace.Settings.PlayersPlaying
		end
		if Workspace.Settings.PlayersScores:FindFirstChild(Player.Name) == nil then
			local NewModel = Instance.new("Model")
			NewModel.Parent = Workspace.Settings.PlayersScores
			NewModel.Name = Player.Name
			for A = 1,18 do
				local NewVal = Instance.new("StringValue")
				NewVal.Parent = NewModel
				NewVal.Name = A
				NewVal.Value = ""
			end
		end

so basically i need to remove PlayerAdded feature and replace it with player touching part function

You can use the touched event from a part like this:

part.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
       --Your function without the "game.Players.PlayerAdded:connect(function(Player)" line
    end
end)

Edit:
Not directly related but note that you can use workspace instead of game.Workspace if you want, removing the first line

1 Like

Thank you!! It works perfectly

1 Like