I am having some problems with a crate opening system. (It was a tutorial but I changed it a lot) I don’t want one of those crate opening systems like MM2 or Jailbreak.
What do you want to achieve? I made a crate opening system, it was a model in the tutorial (The model one works). But I want to turn it into a Gui.
What is the issue? When you click a Gui button, nothing fires. There aren’t any errors or anything.
What solutions have you tried so far? I have searched up on the dev forum about values and stuff. But nothing that I have found, I changed the script but it still doesn’t work.
There aren’t any errors.
Script located in the Gui. (The script is named CrateOpeningScript)
local cost = 500
local BuyBtnCrate1 = script.Parent.AnimalPage.Crate1.BuyBtn
local player = game.Players.LocalPlayer
local Money = player:WaitForChild("Money")
local skinModule = require(game.ServerScriptService:WaitForChild("skinModule"))
BuyBtnCrate1.MouseButton1Click:Connect(function(player)
print("Click")
if player.Money.Value >= cost then
print("If statement fired")
player.Money.Value = player.Money.Value - cost
local skin = skinModule.chooseRandomSkin()
print(skin.Name.." selected")
local skinVal = Instance.new("StringValue")
skinVal.Name = skin.Name
skinVal.Parent = player.SkinInventory
game.ReplicatedStorage.BreakCrate:FireClient(player,skin)
end
end)
I changed it to a local script, but the only error that shows up related to this system is: Infinite yield possible on ‘ServerScriptService:WaitForChild(“skinModule”)’
(This is what the local script looks like, I changed it a little)
local cost = 500
local BuyBtnCrate1 = script.Parent.AnimalPage.Crate1.BuyBtn
local player = game.Players.LocalPlayer
local Money = player:WaitForChild("Money")
local skinModule = require(game.ServerScriptService:WaitForChild("skinModule"))
BuyBtnCrate1.MouseButton1Click:Connect(function(player)
print("Click")
if player.Money.Value >= cost then
player.Money.Value = player.Money.Value - cost
local skin = skinModule.chooseRandomSkin()
print(skin.Name.." selected")
local skinVal = Instance.new("StringValue")
skinVal.Name = skin.Name
skinVal.Parent = player.SkinInventory
game.ReplicatedStorage.BreakCrate:FireServer(player,skin)
end
end)
Also from reading it over once more, you are now handling remote events and you aren’t securing them (from exploiters). An alternative option is having a Server Script choose the skin as opposed to firing the skin through the remote event. With this, you may also keep your module in Server Script Service as you are accessing it through a Server Script
You would just do the :MouseButton1Click and then right after fire the event to the server to handle everything. (From choosing the skin and equipping it)
I took on board your feedback, but it still doesn’t work. The client fires the server but the server doesn’t for some reason pick it up. It prints “Fired Server!” and Infinite yield possible on ‘ServerScriptService:WaitForChild(“skinModule”)’
My new Client Script
local BuyBtnCrate1 = script.Parent.AnimalPage.Crate1.BuyBtn
local player = game.Players.LocalPlayer
BuyBtnCrate1.MouseButton1Click:Connect(function(player)
game.ReplicatedStorage.ChooseSkin:FireServer(player)
print("Fired Server!")
end)
My new server script
local cost = 500
local skinModule = require(game.ServerScriptService:WaitForChild("skinModule"))
local player = game.Players.LocalPlayer
game.ReplicatedStorage.ChooseSkin.OnServerEvent:Connect(function(player)
print("Picked up remote event on server!")
if player.Money.Value >= cost then
player.Money.Value = player.Money.Value - cost
local skin = skinModule.chooseRandomSkin()
print(skin.Name.." selected")
local skinVal = Instance.new("StringValue")
skinVal.Name = skin.Name
skinVal.Parent = player.SkinInventory
game.ReplicatedStorage.BreakCrate:FireClient(player,skin)
end
end)
Make sure “skinModule” is in ServerScriptService and is a Module Script. Infinite yield runs when you have a :WaitForChild() where the item inside cannot be found (so the script waits forever). Also considering you are firing a remote event back to the client after choosing the skin, I recommend also looking into a remote function. This would allow you to use the return statement as well as only needing to fire one remote.
If I were you I’d put my modules in ReplicatedStorage. Pretty sure it works either way, but I’d still put it in Replicated. Also, why are you doing WaitForChild when you can just do game.ReplicatedStorage(or ServerScriptService).modulename?