Catégories
ROBLOX

ROBLOX suggestions

Des suggestions pour débuter en ROBLOX LUA :
créer un commentaire dans ton code,
rechercher une ressource,
détecter un click d’une souris, déplacer un part,
pousser un objet,
synchroniser avec le moteur ROBLOX,
tester l’appartenance d’un objet à une classe,
tester si la collision vient d’un joueur,
modifier la santé du joueur,
modifier vitesse du joueur,
détecter une collision du joueur avec un objet Roblox,
détecter une fin de collision avec le joueur,
réduire la santé du joueur,
comment coder un ClickDetector,
comment coder un Prompt,
créé un système de message RemoteEvent,
ajouter dans l’inventaire,
créer un tableau de score,
comment intercepter une erreur,
rotation d’un objet,
intercepter une touche de clavier

Commentaire d’une ligne :

--print("instruction en commentaire")

Commentaire de plusieurs instructions :

--[[
print("instruction en commentaire")
print("instruction en commentaire")
--]]

Recherche d’une ressource dans un script :

-- recherche d'un objet sous la même arborescence que le script
local btOpenMenu = script.Parent.OpenButton

-- recherche d'un part sous workspace
local myPart = workspace.part

-- recherche de plusieurs part sous un folder
local myParts = workspace.Folder:GetChildren()

-- recherche des joueurs et du joueur local dans un localScript
local players = game:GetService("Players")
local player = players.LocalPlayer

-- récupération de tous les parts dans le workspace
for _, part in ipairs(myParts ) do

end

Comment détecter un click de la souris sur un bouton :

btOpenMenu.MouseButton1Click:Connect(function()

end)

Comment déplacer un objet Roblox :

myPart.Position += Vector3.new(10,0,0)

Déplacer un objet x fois :

for i = 1, 50 do
   maPart.Position += Vector3.new(5,0,0)
   wait(1)
end

Déplacer un objet Roblox tout le temps :

while true do
    maPart.Position += Vector3.new(0.05,0,0)
    wait(0.01)
end

Déplacer un objet jusqu’à une position :

while maPart.Position.X<60 do
   maPart.Position += Vector3.new(5,0,0)
   wait(0.01)
end

Pouser un objet :

part.AssemblyLinearVelocity = Vector3.new(0, 0, 100)

Synchroniser le déplacement avec le moteur de Roblox :

game:GetService("RunService").Heartbeat:Connect(function()
    maPart.Position += Vector3.new(0.05,0,0)
end)

Comment tester l’appartenance d’un objet à une classe :

if otherPart:IsA("Part") then
	print("Part : ",otherPart.Name)

Comment tester si la collision vient d’un joueur :

part.Touched:Connect(function(hit)
    -- 1. Remonte jusqu'au Model parent (le Character)
    local character = hit:FindFirstAncestorOfClass("Model")
    if not character then return end

    -- 2. Cherche le Humanoid dans le Character
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if not humanoid or humanoid.Health <= 0 then return end

    -- 3. Retrouve le Player depuis le Character
    local player = game:GetService("Players"):GetPlayerFromCharacter(character)
    if not player then return end

    print("Joueur touché :", player.Name)
end)

Comment modifier la santé du joueur :

local humanoid = otherPart.Parent and otherPart.Parent:FindFirstChild("Humanoid")
if not humanoid then return end

humanoid.Health = 0

Maximum de santé :

humanoid.MaxHealth

Comment modifier vitesse du joueur :

local humanoid = otherPart.Parent and otherPart.Parent:FindFirstChild("Humanoid")
if not humanoid then return end

humanoid.WalkSpeed = 20

Comment détecter une collision du joueur avec un objet Roblox :

maPart.Touched:Connect(function(otherPart)

    local humanoid = otherPart.Parent and otherPart.Parent:FindFirstChild("Humanoid")
    if not humanoid then return end

Comment détecter une fin de collision avec le joueur :

maPart.TouchEnded:Connect(function(otherPart)

    local humanoid = otherPart.Parent and  
                 otherPart.Parent:FindFirstChild("Humanoid")
    if not humanoid then return end

end)

Santé du joueur à 100 au départ du jeu :

print(humanoid.Health)

Réduire la santé du joueur :

humanoid.Health = humanoid.Health – 5
ou
humanoid:TakeDamage((5))

Le joueur meurt quand sa santé est à zéro :

humanoid.Health = 0

Comment coder un ClickDetector sur le part :

local clickDetector = script.Parent.ClickDetector

clickDetector.MouseClick:Connect(function()
print(« touché »)
end)

Comment coder un Prompt :

maPart.ProximityPrompt.Triggered:Connect(function(otherPart)
print(‘activé’)
end)

Créé un système de message RemoteEvent

Sous ReplicatedStorage crée un RemoteEvent

Dans un localScript :

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent:FireServer(param1, param2, ...)

Puis dans un script serveur :

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent .OnServerEvent:Connect(function(player, param1, param2, ...)

end)

Ajouter dans l’inventaire :

item.Parent = player.Backpack

Créer un tableau de score :

Insérer un script leaderstats sous ServerScriptService :

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	-- Exemple de valeurs du tableau
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 0
end)

Puis pour modifier le score dans un script serveur :

local player =
               game:GetService("Players"):
               GetPlayerFromCharacter(otherPart.Parent)
if not player then return end	
	
player:WaitForChild("leaderstats").Coins.Value += 1

Comment intercepter une erreur par exemple sur « jouer un son » :

	local success, err = pcall(function()
		sound:Play()
	end)
	if not success then
		warn("Erreur lors de la lecture du son :", err)
	end

Rotation d’un objet :

local mypart = script.Parent

mypart.Touched:Connect(function(hit)
	mypart:PivotTo(mypart:GetPivot() *
                       CFrame.Angles(math.rad(90), 0, 0))
end)

Intercepter une touche de clavier :

Dans un LocalScript sous StarterPlayer StarterPlayerScripts

local UserInputService = game:GetService("UserInputService")

print("clavier bbb")
-- Connecte l'événement
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode ~= Enum.KeyCode.Unknown then
		print("Clavier", input.KeyCode)

		if input.KeyCode == Enum.KeyCode.X and not gameProcessed then
			print("Appui X")
		end

	end

end)