This is a regular script inside of a weapon, in a box i'm using as a weapon giver. The script works correctly when on a desktop, it gives you the "Pistol". The script also works correctly on touch devices when using "Play Here" on studio. On Local server testing or live i still am receiving the regular "Pistol", and not the desired "MobilePistol". Ive checked through the code and everything seems fine, any ideas? Thanks in advance!
local debounce = false local inputservice = game:GetService("UserInputService") local gun = game:GetService("ReplicatedStorage").VBuckWeapons.Common.Pistol -- Location of gun local mobgun = game:GetService("ReplicatedStorage").VBuckWeapons.Common.MobilePistol -- Location of gun local debouncetime = 2 -- Time of debounce function onTouch(hit) if hit.Parent:FindFirstChild("Humanoid") then local plr = game:GetService("Players"):FindFirstChild(hit.Parent.Name) local char = plr.Character if inputservice.KeyboardEnabled == false then debounce = true print("You are playing in android") if char:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(mobgun.Name) or char:FindFirstChild(mobgun.Name) then print("You already have a gun!") wait(debouncetime) debounce = false else mobgun:Clone().Parent = plr.Backpack wait(debouncetime) debounce = false end else debounce = true gun:Clone().Parent = plr.Backpack print("You playing in computer") wait(debouncetime) debounce = false return false; end elseif hit.Parent.Parent:FindFirstChild("Humanoid") then local plr = game:GetService("Players"):FindFirstChild(hit.Parent.Parent.Name) local char = plr.Character if inputservice.KeyboardEnabled == false then debounce = true print("You are playing in android") if char:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(mobgun.Name) or char:FindFirstChild(mobgun.Name) then print("You already have a gun!") wait(debouncetime) debounce = false else mobgun:Clone().Parent = plr.Backpack wait(debouncetime) debounce = false end else debounce = true gun:Clone().Parent = plr.Backpack print("You playing in computer") wait(debouncetime) debounce = false return false; end else return false; end end script.Parent.Parent.Touched:connect(onTouch)
EDIT
-- Variables -- local debounce = false local debouncetime = 2 game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(c) local v = Instance.new("BoolValue", c) v.Name = "Touched" v.Value = false end) end) function touch(hit) if hit.Parent:FindFirstChild("Humanoid") and debounce == false and hit.Parent:FindFirstChild("Touched") then debounce = true local char = hit.Parent char.Touched.Value = true wait(debouncetime) char.Touched.Value = false debounce = false else return end end script.Parent.Touched:Connect(touch)
Not need functiont o get player, not use "else if" use "elseif" for detect player input you need to make in a local script, and make RemoveEvent, script
Here is code:
PART CODE
Create a Server Script(Script)
and put in your Part
And put this code:
-- Variables -- local debounce = false local debouncetime = 2 game.Players.PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(c) local v = Instance.new("BoolValue", c) v.Name = "Touched" v.Value = false end) end) function touch(hit) if hit.Parent:FindFirstChild("Humanoid") and debounce == false and hit.Parent:FindFirstChild("Touched") then debounce = true local char = hit.Parent char.Touched.Value = true wait(debouncetime) char.Touched.Value = false debounce = false elseif hit.Parent.Parent:FindFirstChild("Humanoid") and debounce == false and hit.Parent.Parent:FindFirstChild("Touched") then debounce = true local char = hit.Parent.Parent char.Touched.Value = true wait(debouncetime) char.Touched.Value = false debounce = false end end script.Parent.Touched:Connect(touch)
EVENT CODE
Now create a event named as GunGiveEvent
, put GunGiveEvent
in ReplicatedStorage
Create a Server Script(Script)
and put in ServerScriptService
In this script put this code:
game.ReplicatedStorage.GunGiveEvent.OnServerEvent:Connect(function(plr,gun) if plr.Character:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) then return false; else gun:Clone().Parent = plr.Backpack end end)
Now create a local script
and put in StarterGui
And put this code:
repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character local gun = game:GetService("ReplicatedStorage").XXXX.Rare.MobilePistol -- Location of gun local service = game:GetService("UserInputService") local plr = game.Players.LocalPlayer local char = plr.Character local block = char:FindFirstChild("Touched") local event = game:GetService("ReplicatedStorage").GunGiveEvent if block then block.Changed:Connect(function() if block.Value == true then if service.TouchEnabled == true and service.KeyboardEnabled == false then print("ANDROID") game:GetService("ReplicatedStorage").GunGiveEvent:FireServer(gun) else print("COMPUTER") end else return; end end) end
Errors? call me in the comments