-
What do you want to achieve? i want to make character switching script
-
What is the issue? when i press Z or X to swapped character nothing happend
-
What solutions have you tried so far? I tried asking the AI to fix the code, but nothing worked.
I want to make my own Genshin impact or Honkai:Star Rail switching system, but nothing happens when I press Z or X. The most confusing thing is when I look at the output, the code works perfectly.
-- ReplicatedStorage.CharacterSwitchingModule
local CharacterSwitchingModule = {}
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
-- RemoteEvent for switching (client-server communication)
local SwitchEvent = game.ReplicatedStorage.SwitchCharacterEvent
-- Cooldown time in seconds
local SWITCH_COOLDOWN = 1.5
local PlayerData = {} -- Stores characters and cooldowns
function CharacterSwitchingModule.InitializePlayer(player, characterList)
PlayerData[player.UserId] = {
Characters = characterList,
CurrentIndex = 1,
LastSwitch = 0 -- Cooldown tracking
}
-- Set first character active
local firstCharacter = characterList[1]
firstCharacter.Parent = workspace
end
function CharacterSwitchingModule.SwitchCharacter(player, nextIndex)
local data = PlayerData[player.UserId]
if not data then return end
local currentTime = tick()
if currentTime - data.LastSwitch < SWITCH_COOLDOWN then
return -- Cooldown active, do nothing
end
data.LastSwitch = currentTime -- Update cooldown timer
local currentChar = data.Characters[data.CurrentIndex]
local nextChar = data.Characters[nextIndex]
if currentChar and nextChar and currentChar ~= nextChar then
-- Store current position before hiding old character
local position = currentChar:GetPrimaryPartCFrame()
-- Hide old character
currentChar.Parent = ReplicatedStorage
-- Spawn visual effect at the switch location
local effect = Instance.new("Part")
effect.Size = Vector3.new(4, 4, 4)
effect.Shape = Enum.PartType.Ball
effect.Material = Enum.Material.Neon
effect.Color = Color3.fromRGB(255, 244, 92)
effect.Position = position.Position
effect.Anchored = true
effect.CanCollide = false
effect.Parent = workspace
Debris:AddItem(effect, 0.5) -- Remove effect after 0.5s
-- Activate new character and keep it in place
nextChar.Parent = workspace
nextChar:SetPrimaryPartCFrame(position)
-- Update current index
data.CurrentIndex = nextIndex
-- Notify client UI update
SwitchEvent:FireClient(player, nextIndex)
end
end
return CharacterSwitchingModule
ServerScript Code
local CharacterSwitchingModule = require(game.ReplicatedStorage.Modules:WaitForChild("CharacterSwitchingModule"))
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
-- Example characters (Replace with actual models)
local char1 = game.ReplicatedStorage.CharacterModels:WaitForChild("Demian"):Clone()
local char2 = game.ReplicatedStorage.CharacterModels:WaitForChild("Yamakuze"):Clone()
-- Set up characters
char1.Parent = game.Workspace
char2.Parent = game.ReplicatedStorage.CharacterModels -- Start hidden
-- Assign characters to player
CharacterSwitchingModule.InitializePlayer(player, {char1, char2})
end)
-- Listen for character switch requests
game.ReplicatedStorage.SwitchCharacterEvent.OnServerEvent:Connect(function(player, nextIndex)
CharacterSwitchingModule.SwitchCharacter(player, nextIndex)
end)
Local script in StarterPlayerScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local switchEvent = ReplicatedStorage:WaitForChild("SwitchCharacterEvent")
local currentIndex = 1
local maxCharacters = 2 -- Change if adding more characters
local switchCooldown = 1.5
local lastSwitchTime = 0
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
local currentTime = tick()
if currentTime - lastSwitchTime < switchCooldown then
return -- Cooldown active
end
lastSwitchTime = currentTime -- Update cooldown timer
if input.KeyCode == Enum.KeyCode.Z then
currentIndex = (currentIndex - 1 < 1) and maxCharacters or (currentIndex - 1)
switchEvent:FireServer(currentIndex)
elseif input.KeyCode == Enum.KeyCode.X then
currentIndex = (currentIndex + 1 > maxCharacters) and 1 or (currentIndex + 1)
switchEvent:FireServer(currentIndex)
end
end)
-- UI Update
switchEvent.OnClientEvent:Connect(function(newIndex)
print("Switched to character: " .. newIndex) -- Replace with UI logic
end)