RemoteEvent Script Help

Hey everyone! :wave:

I’m currently trying to make a script so that when a RemoteEvent is fired, it changes a part in the tools’ transparency and changes the name of the tool.

The error that I’m experiencing is that it ONLY works on the first few lines of script for the VanillaCupcake but doesn’t work for the rest and is not outputting any errors. Could anyone provide assistance with this?

Script:

game.ReplicatedStorage.ChocIcing.OnServerEvent:Connect(function(player)
		if player.Character:FindFirstChild("VanillaCupcake") then
			player.Character.VanillaCupcake.Handle.Cup1.Transparency = 0
			player.Character.VanillaCupcake.Handle.Cup1.BrickColor = BrickColor.new("Brown")
		player.Character.VanillaCupcake.Name = "Vanilla Cupcake Chocolate Icing"
		elseif player.Character:FindFirstChild("ChocolateCupcake") then
			player.Character.ChocolateCupcake.Handle.Cup1.Transparency = 0
			player.Character.ChocolateCupcake.Handle.Cup1.BrickColor = BrickColor.new("Brown")
		player.Character.ChocolateCupcake.Name = "Chocolate Cupcake Chocolate Icing"
		elseif player.Character:FindFirstChild("StrawberryCupcake") then
			player.Character.StrawberryCupcake.Handle.Cup1.Transparency = 0
			player.Character.StrawberryCupcake.Handle.Cup1.BrickColor = BrickColor.new("Brown")
			player.Character.StrawberryCupcake.Name = "Strawberry Cupcake Chocolate Icing"
			elseif player.Character:FindFirstChild("LemonCupcake") then
				player.Character.LemonCupcake.Handle.Cup1.Transparency = 0
				player.Character.LemonCupcake.Handle.Cup1.BrickColor = BrickColor.new("Brown")
				player.Character.LemonCupcake.Name = "Lemon Cupcake Chocolate Icing"
				elseif player.Character:FindFirstChild("CarrotCupcake") then
					player.Character.CarrotCupcake.Handle.Cup1.Transparency = 0
					player.Character.CarrotCupcake.Handle.Cup1.BrickColor = BrickColor.new("Brown")
					player.Character.CarrotCupcake.Name = "Carrot Cupcake Chocolate Icing"
				end
	end)

Thank you in advance for your assistance with this! Much appreciated. :grinning_face_with_smiling_eyes:

The script is a ServerScript. ^

Okay here’s one word of advice, use variables.
I cannot read this.

I think the problem lies with you using elseif instead of if for each statement.
I also recommend you properly indent your code.

1 Like

I’ll do so now, thanks. (30 ch.rs)

@amadeupworld2 I’ll have to correct you. The OP is using the conditional statements correctly. Elseif is conditional keyword that refers to another case with different conditions, while else refers a case where other conditions cannot be met, resulting in this case.

@OP, your index is a problem. You cannot index with a number (Handle.Cup1). To fix this, use brackets:

player.Character.ChocolateCupcake.Handle["Cup1"].Transparency = 0
player.Character.ChocolateCupcake.Handle["Cup1"].BrickColor = BrickColor.new("Brown")

I’ll try this, thank you! (30 ch.rssssss)

Hm, this still hasn’t fixed it.

Mind sending the updated script?

game.ReplicatedStorage.ChocIcing.OnServerEvent:Connect(function(player)
		if player.Character:FindFirstChild("VanillaCupcake") then
			player.Character.VanillaCupcake.Handle["Cup1"].Transparency = 0
			player.Character.VanillaCupcake.Handle["Cup1"].BrickColor = BrickColor.new("Brown")
		player.Character.VanillaCupcake.Name = "Vanilla Cupcake Chocolate Icing"
	elseif player.Character:FindFirstChild("ChocolateCupcake") then
			player.Character.ChocolateCupcake.Handle["Cup1"].Transparency = 0
			player.Character.ChocolateCupcake.Handle["Cup1"].BrickColor = BrickColor.new("Brown")
		player.Character.ChocolateCupcake.Name = "Chocolate Cupcake Chocolate Icing"
		elseif player.Character:FindFirstChild("StrawberryCupcake") then
			player.Character.StrawberryCupcake.Handle["Cup1"].Transparency = 0
			player.Character.StrawberryCupcake.Handle["Cup1"].BrickColor = BrickColor.new("Brown")
			player.Character.StrawberryCupcake.Name = "Strawberry Cupcake Chocolate Icing"
			elseif player.Character:FindFirstChild("LemonCupcake") then
				player.Character.LemonCupcake.Handle["Cup1"].Transparency = 0
				player.Character.LemonCupcake.Handle["Cup1"].BrickColor = BrickColor.new("Brown")
				player.Character.LemonCupcake.Name = "Lemon Cupcake Chocolate Icing"
				elseif player.Character:FindFirstChild("CarrotCupcake") then
					player.Character.CarrotCupcake.Handle["Cup1"].Transparency = 0
					player.Character.CarrotCupcake.Handle["Cup1"].BrickColor = BrickColor.new("Brown")
					player.Character.CarrotCupcake.Name = "Carrot Cupcake Chocolate Icing"
	end
end)

Edit: Also, I didn’t think this would be the issue as it works for only the VanillaCupcake perfectly fine. Keep that factor in mind.

Honestly, I would really recommend you do FindFirstChildOfClass("Tool") instead of what you were doing since form the looks of it, they’re tools. You’re doing a lot o repetition, so this’ll save you some lines. And for the name changing? Just remove the last 7 characters and do some concatenation to change the name for this case. Your code can be easily condensed to this

game.ReplicatedStorage.ChocIcing.OnServerEvent:Connect(function(player)
    local char = player.Character
    local cupcake = char:FindFirstChildOfClass("Tool")
    
    if cupcake and cupcake.Handle:FindFirstChild("Cup1") then
        cupcake.Handle.Cup1.Transparency = 0
        cupcake.Handle.Cup1.BrickColor = BrickColor.new("Brown")
        local name = string.gsub(cupcake.Name, " ", "")
        local cupcakeWord = name:find("Cupcake")
        if not cupcakeWord then return end
        local flavour = name:sub(1,cupcakeWord-1)
        cupcake.Name = flavour .. " ".. "Cupcake Chocolate Icing"
    end
end)

Find a tool in the character, if a tool was found and it has Cup1 in its handle, change the transparency and the color and do some string changing stuff to rename the cup.

If you’re ever repeating yourself, please reach to find better ways to improve the feel of your code

Hopefully this should work, if it doesn’t, please let me know so I can try to help

Edit: Issue was on his end, the edited version works fine

1 Like

The unedited script worked, but when I try to use the edited version it doesn’t work, no errors.

What do you mean by the unedited script?

You know how you edited it after you posted it? Before you edited it it worked, but once you edited it and I changed my script to the edited version it didn’t.

Oh, I think you could use the unedited version then, I think I may’ve messed up the string stuff, even though it should work. Were you referring to the fact that it wont change the name when you switched to the edited version?

Alright! I’ll use the unedited version for now and mark it as a solution. Thank you!

And no, the whole script didn’t function once edited for some reason.

Odd, I think you may’ve miscopied something? The only thing I did was just change the way it changes the name, but it wont be an issue if you can’t change icings.

If you have anymore issues don’t be afraid to make another post!

I’ve figured it out, it was an issue on my end and the edited script now works. Thank you for your help, much appreciated!

1 Like