Catégories
Jeu vidéo ROBLOX

Barrières invisibles infranchissables

Crée dans ton jeu des barrières invisibles que le joueur ne peut pas franchir. Dès que le joueur touche une barrière invisible, celle-ci se matérialise et déclenche une alarme sonore. Tu peux répliquer autant de barrières invisibles et infranchissables que tu auras besoin pour ton jeu. Ces barrières permettent à ton joueur d’être guider et l’oblige à passer par certaines étapes de ton monde.

Crée dans ton workspace l’arborescence suivante dans un Folder avec un fichier son en option, un script, un part représentant un mur :

Puis saisie ce code dans ton script :

local Players = game:GetService("Players")

-- Configuration
local TRANSPARENCY_VISIBLE = 0.7
local TRANSPARENCY_HIDDEN  = 1
local FADE_DELAY           = 2

-- Récupération des objets du folder
local wallsFolder = script.Parent

-- Fonction appelée quand un joueur touche un mur
local function onWallTouched(otherPart, wall)
	-- Vérification joueur uniquement
	local character = otherPart.Parent
	if not Players:GetPlayerFromCharacter(character) then return end
	print("Collision avec le joueur")
end

-- Lecture de tous les murs du dossier
for _, wall in wallsFolder:GetChildren() do
	if not wall:IsA("Part") then continue end

	wall.Anchored     = true
	wall.Transparency = TRANSPARENCY_HIDDEN
	wall.Material     = Enum.Material.Neon
	wall.CanCollide   = true  -- mur bloquant

	wall.Touched:Connect(function(otherPart)
		onWallTouched(otherPart, wall)
	end)
end

Lance ton jeu, le mur est invisible, si ton joueur touche le mur il reste bloqué !!! Un message dans la sortie indique que le joueur est rentré en collision avec le mur :

Afin d’indiquer au joueur qu’il ne peut pas franchir le mur invisible, on va le faire apparaître et déclencher une alarme sonore.

Modifie le script :

local Players = game:GetService("Players")

-- Configuration
local TRANSPARENCY_VISIBLE = 0.7
local TRANSPARENCY_HIDDEN  = 1
local FADE_DELAY           = 2

-- Récupération des objets du folder
local wallsFolder = script.Parent

-- Chargement du fichier sonore (si présent)
local sound = wallsFolder:FindFirstChildOfClass("Sound")
if not sound then
	warn("[CoinManager] Aucun Sound trouvé dans ", coinsFolder.Name)
end

-- Fonction appelée quand un joueur touche un mur
local function onWallTouched(otherPart, wall)
	-- Vérification joueur uniquement
	local character = otherPart.Parent
	if not Players:GetPlayerFromCharacter(character) then return end
	-- Évite les appels multiples simultanés
	if wall.Transparency == TRANSPARENCY_VISIBLE then return end
	
	-- Affichage du mur en transparence
	wall.Transparency = TRANSPARENCY_VISIBLE
	-- Son d'alerte
	if sound then
		pcall(function() sound:Play() end)
	end
	task.wait(FADE_DELAY)
	wall.Transparency = TRANSPARENCY_HIDDEN
end

-- Lecture de tous les murs du dossier
for _, wall in wallsFolder:GetChildren() do
	if not wall:IsA("Part") then continue end

	wall.Anchored     = true
	wall.Transparency = TRANSPARENCY_HIDDEN
	wall.Material     = Enum.Material.Neon
	wall.CanCollide   = true  -- mur bloquant

	wall.Touched:Connect(function(otherPart)
		onWallTouched(otherPart, wall)
	end)
end

Duplique les murs invisibles infranchissables et déplace les dans ton espace de jeu :