local expManager = game.ReplicatedStorage.ExpManager
local UIS = game:GetService("UserInputService") -- Service used to get the key pressed by a player
local block = game.Players.LocalPlayer:WaitForChild("MainWorldCrate")
UIS.InputBegan:Connect(function(input,gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if UIS:GetFocusedTextBox() == nil then -- Checks if the player is typing on a textbox like the chat... If not then...
for i,v in pairs(game.Workspace.Crates["Wood Crate"]:GetChildren()) do -- Finds all the children from the folder. In this case it will find all the NPCs
local mag = (v.Parent.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude -- Makes a variable for the position between the character and the npc.
if mag <= 10 then -- If the magnitude is less than or equal to 10 studs then
--Effect XD
for i = 1,0.6,-0.05 do
wait()
script.Parent.ImageColor3 = Color3.new(i,i,i)
end
wait()
for i = 0.6,1,0.05 do
wait()
script.Parent.ImageColor3 = Color3.new(i,i,i)
end
--This effect is like when you pressed a button it will darken.
if block.Value == true then print("Already Opened!") end
if not block.Value == true then
wait(.2)
expManager:FireServer(0,0,200,500)
block.Value = true
end
end
end
end
end
if input.UserInputType == Enum.UserInputType.Keyboard or input.UserInputType == Enum.UserInputType.Gamepad1 or input.UserInputType == Enum.UserInputType.Gamepad2 or input.UserInputType == Enum.UserInputType.Touch then -- Checks if the player uses a Keyboard
if input.KeyCode == Enum.KeyCode.E or input.KeyCode == Enum.KeyCode.ButtonX then -- Checks if the player pressed the "E" key
if UIS:GetFocusedTextBox() == nil then -- Checks if the player is typing on a textbox like the chat... If not then...
for i,v in pairs(game.Workspace.Crates["Wood Crate"]:GetChildren()) do -- Finds all the children from the folder. In this case it will find all the NPCs
local mag = (v.Parent.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude -- Makes a variable for the position between the character and the npc.
if mag <= 10 then -- If the magnitude is less than or equal to 10 studs then
--Effect XD
for i = 1,0.6,-0.05 do
wait()
script.Parent.ImageColor3 = Color3.new(i,i,i)
end
wait()
for i = 0.6,1,0.05 do
wait()
script.Parent.ImageColor3 = Color3.new(i,i,i)
end
--This effect is like when you pressed a button it will darken.
if block.Value == true then
print("Already Opened!")
end
if not block.Value == true then
wait(.2)
block.Value = true
expManager:FireServer(0,0,200,500)
end
end
end
end
end
end
end)
while wait() do
script.Parent.Visible = false
for i,v in pairs(game.Workspace.Crates["Wood Crate"]:GetChildren()) do -- Finds all the children from the folder. In this case it will find all the NPCs
local lucky = v.Parent
mag = (lucky.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude-- Makes a variable for the position between the character and the npc.
if mag <= 10 then -- If the magnitude is less than or equal to 10 studs then
local WSP = game.Workspace.CurrentCamera:WorldToScreenPoint(lucky.Center.Position) -- The position of the NPC's Torso to 2d
script.Parent.Visible = true
script.Parent.Position = UDim2.new(0,WSP.X,0,WSP.Y)
script.Parent.TextLabel.Text = "Open 'Wood Crates'" -- Refreshes the text and puts the name of the npc
end
end
end
So basically this script will fire a remoteEvent and a script in serverscriptservice will make a math.random value and add the Exp intValue inside the player. But if I spam the 'E' button or MouseButton1 it will give me 10000 exp which is not intended. It is supposed to give a random value [200~500] But why would it do that?

1 comment
Please fix code format and indentation