My entire script is not working. I am trying to cycle through a folder of 3 characters in replicatedstorage to swap between them, kind of like how it would work in the game Sonic Heroes, but the number values never change and therefore the character doesn’t either.
LOCALSCRIPT [[S. CHAR S.]]
local chars = game.ReplicatedStorage.Characters:GetChildren()
local nextChar = 2
local charVal = 2
local remote = game.ReplicatedStorage.Remotes.ChangeChar
function change_character(plr)
warn(charVal)
charVal += 1
if charVal == 4 then
charVal = 1
end
if charVal == 0 then
charVal = 3
end
local cframe = plr.Character.HumanoidRootPart.CFrame
local velocity = plr.Character.HumanoidRootPart.Velocity
local starterPlayer = game.StarterPlayer
local oldChar = starterPlayer:FindFirstChild("StarterCharacter")
if oldChar then
oldChar:Destroy()
end
remote:FireServer(chars[charVal], charVal, cframe, velocity)
end
game.UserInputService.InputBegan:Connect(function(input, gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.E then
change_character(game.Players.LocalPlayer)
end
end)
SERVERSCRIPT [[S. SCRIPT SERVICE]]
local Players = game:GetService("Players")
local remote = game.ReplicatedStorage.Remotes.ChangeChar
local storedNumber
remote.OnServerEvent:Connect(function(plr, nextChar, charVal, cframe, velocity)
local chars = game.ReplicatedStorage.Characters
local newChar = nextChar:Clone()
local storedNumber = charVal
print(storedNumber)
newChar.Name = "StarterCharacter"
newChar.Parent = game.StarterPlayer
plr:LoadCharacter(newChar)
plr.Character:WaitForChild("HumanoidRootPart").CFrame = cframe
plr.Character:WaitForChild("HumanoidRootPart").Velocity = velocity
local newNumber = storedNumber
remote:FireClient(plr, newNumber)
end)
Is the function firing? Is the warn printing the number? What exactly here isn’t working? Are you getting any output errors? Could you send a video of the script running please, thanks.
Well, I tested out your code and it works. It’s just that some numbers are shuffled around. For us to help you with this, you’ll want to show us your server-sided code, because the client doesn’t make any changes to the character.
By the way, if you want your numbers to print properly in the output, I’d recommend doing the following:
local charsfolder = game.ReplicatedStorage.Characters
chars = {charsfolder.Character1, charsfolder.Character2, charsfolder.Character3} -- Instances can sometimes shuffle around on runtime; This is recommended.
local charVal = 1
local remote = game.ReplicatedStorage.Remotes.ChangeChar
function change_character(plr)
charVal += 1
if charVal == 4 then
charVal = 1
end
warn(charVal) -- note that the warning is now further ahead in your code.
(...)
With these changes, you’ll print the correct information.
I was just going to say that! I tested with your code, and you’re right. What’s going on is that the :LoadCharacter() function resets your character every time it runs, therefore consistently resetting your client-side charVal variable back to 1. To fix this, you’ll simply want to place your script in StarterPlayerScripts. It should work (*as long as your character is loaded).
Huh, I just ran into another issue. My value is changing, and so is the value of newChar. Everything is working as i want… but only in the print section. My character is not changing at all. However, in StarterPlayer, everything is changing as intended? I think I’m doing something wrong… (I mean that in Workspace nothing is changing even though i can see the character being destroyed)
Thanks, I didn’t notice it. What’s going on here is that the server is consistently creating new clones of characters, and parenting them to StarterPlayer (you can view this through the server).